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. listview 两个Item可以同时点击

    android:splitMotionEvents="false" ListView的这个属性可以限制它不能同时点击两个Item

  2. 从头认识java-15.7 Map(5)-介绍HashMap的工作原理-Key变了,能不能get出原来的value?(偶尔作为面试题)

    这一章节我们讨论一个比較特殊的情况Key变了,能不能get出原来的value? 答案是:有时能够,有时不能够 1.能够的情况: package com.ray.ch14; import java.ut ...

  3. django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板

    目录 项目介绍和源码: 拿来即用的bootstrap模板: 服务器SSH服务配置与python中paramiko的使用: 用户登陆与session; 最简单的实践之修改服务器时间: 查看和修改服务器配 ...

  4. C++11 override和final

    30多年来,C++一直没有继承控制关键字.最起码这是不容易的,禁止一个类的进一步衍生是可能的但也很棘手.为避免用户在派生类中重载一个虚函数,你不得不向后考虑. C++ 11添加了两个继承控制关键字:o ...

  5. 如何让 zend studio 10 识别 Phalcon语法并且进行语法提示

    让 zend studio 10 识别 Phalcon语法并且进行语法提示 https://github.com/rogerthomas84/PhalconPHPDoc 下载解压后,把里面 phalc ...

  6. Eclipse - Mac Os Default JRE missing

    转:http://stackoverflow.com/questions/1736993/eclipse-mac-os-default-jre-missing 1) Follow Joshua's a ...

  7. 标准JAVA MD5方法

    https://blog.csdn.net/wangfei0904306/article/details/71565968 ************************************** ...

  8. Win10+VMplayer12中U盘无法挂载解决

    VMplayer 中运行mac osx,想挂载个U盘进去,无奈居然一直挂载不进去,而此时宿主机Win10中的U盘也消失,查看设备管理器居然显示"未知设备" 于是乎一顿度娘,网上的方 ...

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

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

  10. tf.Variable

    tf.Variable __init__( initial_value=None, trainable=True, collections=None, validate_shape=True, cac ...