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. nginx 404 403等错误信息页面重定向到网站首页或其它事先指定的页面

    server { listen 80; server_name www.espressos.cn; location / { root html/www; index index.html index ...

  2. 菜鸟学Java(七)——Ajax+Servlet实现无刷新下拉联动

    下拉联动的功能可以说非常的常用,例如在选择省.市等信息的时候:或者在选择大类.小类的时候.总之,下拉联动很常用.今天就跟大家分享一个简单的二级下拉联动的功能. 大类下拉框:页面加载的时候就初始化大类的 ...

  3. query compiler

    https://db.in.tum.de/teaching/ws1415/queryopt/chapter3.pdf?lang=de pi3.informatik.uni-mannheim.de/~m ...

  4. js中实现对checkbox选中和取消

    可以使用 element.attr('checked','checked') 来进行选中.但是不能使用 element.attr('checked','false') 来取消选中. 必须通过以下方式: ...

  5. c--日期和时间函数

    C的标准库<time.h>包含了一些处理时间与日期的函数. 1.clock_t clock(void); 函数返回程序自开始执行后的处理器时间,类型是clock_t,单位是tick.如果有 ...

  6. python(45)内置函数:os.system() 和 os.popen()

    os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ...

  7. 对于iOS开发人工智能意味着什么

    对于iOS开发人工智能意味着什么? 前言 近几年来人工智能的话题那是炙手可热.在国内很多大佬言必谈机器学习和大数据:在美国刚毕业的人工智能 PHD 也是众人追捧,工资直逼 NFL 四分卫.人工智能甚至 ...

  8. 李洪强iOS开发之静态库的打包一

    李洪强iOS开发之静态库的打包一 //静态库一般做一下几种事情 //1 工具类 算法逻辑 新建工具类LHQTools 定义类方法 + (NSInteger)sumWithNum1: (NSIntege ...

  9. WebService 获取当前URL

    private string pageRoot = string.Empty; public virtual string PortalRoot { get { if (pageRoot == nul ...

  10. Natural Language Processing, 2017, Mar.29, Weekly Report

    Distributed Representations of Words and Phrases and their Compositionality T Mikolov, I Sutskever, ...