1. Measure Arrange这两个方法是UIElement的方法

MeasureOverride ArrangeOverride这两个方法是FrameworkElement的方法,FrameworkElement是UIElement的子类

MeasureOverride传入父容器分配的可用空间,返回该容器根据其子元素大小计算确定的在布局过程中所需的大小。

ArrangeOverride传入父容易最终分配的控件大小,返回使用的实际大小

2. MeasureOverride 用于计算本身及其子控件的大小

ArrangeOverride用于布局本身及其子控件的位置和大小

3. WPF布局系统大概分为两步:Measure和Arrange

Measure方法自顶而下,递归调用各子控件的Measure方法,Measure方法会把该控件所需的大小控件存在desired size属性中,控件根据各子控件的desired size 属性确定自身空间大小,并返回自己的desired size

Arrange方法发生在Measure中,传入Measure方法计算到的大小,利用控件的位置设置分配子控件的位置

简单来说,这两个方法一个管大小,一个管布局,都需要调用子类的Measure和Arrage

public class DiagnolPanel:Panel
{
protected override Size MeasureOverride(Size availableSize)
{
var mySize = new Size(); foreach (UIElement child in this.InternalChildren)
{
child.Measure(availableSize);
mySize.Width += child.DesiredSize.Width;
mySize.Height += child.DesiredSize.Height;
} return mySize;
} protected override Size ArrangeOverride(Size finalSize)
{
var location = new Point(); int childNumber = ;
int middleChild = GetTheMiddleChild(this.InternalChildren.Count); foreach (UIElement child in this.InternalChildren)
{ if (childNumber < middleChild)
{
child.Arrange(new Rect(location, child.DesiredSize));
location.X += child.DesiredSize.Width;
location.Y += child.DesiredSize.Height;
}
else
{
//The x location will always keep increasing, there is no need to take care of it
location.X = GetXLocationAfterMiddleChild(childNumber); //If the UIElements are odd in number
if (this.InternalChildren.Count % != )
{
//We need to get the Y location of the child before middle location,
//to have the same Y location for the child after middle child
int relativeChildBeforeMiddle = middleChild - (childNumber - middleChild);
location.Y = GetYLocationAfterMiddleChild(relativeChildBeforeMiddle);
}
else
{
///TODO: Do the design for the even number of children
} child.Arrange(new Rect(location, child.DesiredSize));
} childNumber++;
} return finalSize;
} private double GetXLocationAfterMiddleChild(int childNUmber)
{
double xLocation = ;
for (int i = ; i < childNUmber; i++)
{
xLocation += this.InternalChildren[i].DesiredSize.Width;
} return xLocation;
} private double GetYLocationAfterMiddleChild(int relativeChildNumber)
{
UIElement correspondingChild = this.InternalChildren[relativeChildNumber - ];
Point pointCoordinates =
correspondingChild.TransformToAncestor((Visual)this.Parent).Transform(new Point(, )); return pointCoordinates.Y;
} private int GetTheMiddleChild(int count)
{
int middleChild;
if (count % == )
{
middleChild = count / ;
}
else
{
middleChild = (count / ) + ;
} return middleChild;
}
}
}
<local:DiagnolPanel>
<Button BorderBrush="Black" Background="Red" Content="" Width=""></Button>
<Button BorderBrush="Black" Background="Red" Content="" Width=""></Button> <Button BorderBrush="Black" Background="Red" Content="" Width=""></Button>
<Button BorderBrush="Black" Background="Red" Content="" Width=""></Button> <Button BorderBrush="Black" Background="Red" Content="" Width=""></Button> <Button BorderBrush="Black" Background="Red" Content="" Width=""></Button> <Button BorderBrush="Black" Background="Red" Content="" Width=""></Button>
<Button BorderBrush="Black" Background="Red" Content="" Width=""></Button> <Button BorderBrush="Black" Background="Red" Content="" Width=""></Button> </local:DiagnolPanel>

原文地址:http://www.mamicode.com/info-detail-1730861.html

    https://www.codeproject.com/Articles/1034445/Understanding-MeasureOverride-and-ArrangeOverride

【转】【WPF】WPF中MeasureOverride ArrangeOverride 的理解的更多相关文章

  1. WPF中MeasureOverride ArrangeOverride 的理解

    1. Measure Arrange这两个方法是UIElement的方法 MeasureOverride ArrangeOverride这两个方法是FrameworkElement的方法,Framew ...

  2. 浅谈WPF本质中的数据和行为

    WPF缩写为Windows Presentation Foundation的缩写,本文所要谈的就是WPF本质中的数据和行为,希望通过本文能对大家了解WPF本质有所帮助. 如果自己来做一个UI框架,我们 ...

  3. WPF/Silverlight中的RichTextBox总结

    WPF/Silverlight中的RichTextBox总结   在WPF或者是在Silverlight中有个非常强大的可以编辑的容器控件RichTextBox,有的时间会采取该控件来作为编辑控件.鉴 ...

  4. WPF: WPF 中的 Triggers 和 VisualStateManager

    在之前写的这篇文章 WPF: 只读依赖属性的介绍与实践 中,我们介绍了在 WPF 自定义控件中如何添加只读依赖属性,并且使其结合属性触发器 (Trigger) 来实现对控件样式的改变.事实上,关于触发 ...

  5. 在WPF程序中使用摄像头兼谈如何使用AForge.NET控件(转)

    前言: AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理.神经网络.遗传算法和机器学习等.在C#程序中使用摄像头,我习惯性使用AForge.NET提供的类库.本 ...

  6. WPF 程序中启动和关闭外部.exe程序

    当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...

  7. 如何在WPF程序中使用ArcGIS Engine的控件

    原文 http://www.gisall.com/html/47/122747-4038.html WPF(Windows Presentation Foundation)是美国微软公司推出.NET ...

  8. WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示

    原文:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略. 运行时效果图:XAML代码:// Transform.XAML< ...

  9. WPF程序中App.Config文件的读与写

    WPF程序中的App.Config文件是我们应用程序中经常使用的一种配置文件,System.Configuration.dll文件中提供了大量的读写的配置,所以它是一种高效的程序配置方式,那么今天我就 ...

随机推荐

  1. centos 7 五笔安装

    # yum install ibus ibus-table-wubi 需要重启动

  2. CCZone

    /**************************************************************************** Copyright (c) 2010 coc ...

  3. Kprobes

    https://landley.net/kdocs/ols/2007/ols2007v1-pages-215-224.pdf https://www.kernel.org/doc/Documentat ...

  4. 基于tcpdump的Android智能移动终端数据包捕获完整解决方案

    如何在Android智能手机上捕获数据包? 本文由CSDN-蚍蜉撼青松[主页:http://blog.csdn.net/howeverpf]原创,转载请注明出处! 当前Android系统越来越流行,无 ...

  5. libevent源码分析:event_add、event_del

    event_add.event_del两个函数分别是使event生效和失效的,下面就来看一下两个函数的实现. event_add int event_add(struct event *ev, con ...

  6. Docker 镜像操作

    列出镜像列表 我们可以使用 docker images 来列出本地主机上的镜像. runoob@runoob:~$ docker images REPOSITORY TAG IMAGE ID CREA ...

  7. STM32——项目需求之低功耗的停机模式

    在说低功耗之前,先要明白一个东西,那就是stm32中的事件和中断. 事件是中断的触发源,开放了对应的中断屏蔽位,则事件可以触发相应的中断.在STM32中,中断与事件不是等价的,一个中断肯定对应一个事件 ...

  8. 判断URL文件是不是在于在。

    判断URL文件是不是在于在. private static bool UrlIsExist(string url) { System.Uri u = null; try { u = new Uri(u ...

  9. [转]Oracle存储过程总结

    原文地址:https://www.cnblogs.com/tingbogiu/p/5641000.html 1.存储过程结构 1.1 第一个存储过程 create or replace procedu ...

  10. 封装篇——图片模块(Glide)

    如今市面上差点儿全部的app都用到了图片,图片模块的开发是app开发中不可缺少的一块工作, 开源的力量是强大的.好多优秀的第三方项目能够任君使用,帮助我们提高效率.而且不须要反复造轮子,这边我採用的是 ...