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. Android 相关的资源

    源码分析: http://blog.csdn.net/luoshengyang/article/details/8923485 中文博客: 英文博客: https://github.com/andro ...

  2. ExtJs--05--给window组件加入功能条以及子组件获取上级或下级组件的属性和方法

    Ext.onReady(function(){ /** 1-- 给容器组件加入控制条 及 控制项 控制条 不同的方向有多种 tbar lbar rbar bbar fbar 2-- 依据组件本身拿到上 ...

  3. 【驱动】Flash设备驱动基础·NOR·NAND

    Flash存储器 ——>Flash存储器是近几年来发展最快的存储设备,通常也称作闪存.Flash属于EEPROM(电可擦除可编程只读存储器),是一类存取速度很高的存储器. ——>它既有RO ...

  4. 使用线性回归识别sklearn中的手写数字digit

    从昨天晚上,到今天上午12点半左右吧,一直在调这个代码.最开始训练的时候,老是说loss:nan 查了资料,因为是如果损失函数使用交叉熵,如果预测值为0或负数,求log的时候会出错.需要对预测结果进行 ...

  5. poj1733(区间上的种类并查集)

    题目大意是:一个由0,1组成的数字串~~,现在你问一个人,第i位到第j位的1的个数为奇数还是偶数.一共会告诉你几组这样的数 要你判断前k组这个人回答的都是正确的,到第k+1组,这个人说的是错的,要你输 ...

  6. pual_bot 天气插件编写

    最近在玩pual_bot,感觉很不错,最近天气插件失效了,就结合百度api重新写了一个,也提交了. https://github.com/coldnight/pual_bot #!/usr/bin/e ...

  7. Spring Batch并行与扩展

    1. 概述 Spring Batch提供了多种方式用于处理并行,提高性能.主要分为2大类: - 单个进程,多线程 - 多个进程 因此,可以细分为以下几类: - 多线程Step(Multi-thread ...

  8. 【转】26张PPT让你告别拖延症

    不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在. 可能的话,请仔细读读PPT原件而不要只是看翻译吧. 1.时间常有,时间优先. 2 ...

  9. s9303这样的arp表是代表什么意思?

    s9303这样的arp表是代表什么意思? 在s9303交换机下dis arp 看到了最末2条有这样的记录 那个Incomplete 是什么意思呢? 答: 如果该字段显示为“Incomplete”,表示 ...

  10. openvpn之server配置篇

    openvpn server的配置路径下有大约如下文件: [root@localhost server]# ll total -rw-r-----. nobody nobody Sep ca.crt ...