Understanding the WPF Layout System
Many people don't understand how the WPF layout system works, or how that knowledge can help them in their projects. I intend to shine a little light on the mechanics behind all those cool layout controls in WPF.
To explain the system, I will give a step-by-step example of creating a custom panel for WPF. This panel will be similar to the StackPanel, with the difference that it will expand the last child to fill the remaining space.
Step 1: Create a new project
Create a new WPF Application project called WpfLayoutSystem.
Step 2: Add the custom panel
Add a new class to the project. Call it ExpandingStackPanel.cs.
To create your own custom panel, create a class that inherits from Panel, and override MeasureOverride and ArrangeOverride. That's it. This is the most basic panel, which doesn't add any functionality, yet.
using System;
using System.Windows;
using System.Windows.Controls; namespace WpfLayoutSystem
{
class ExpandingStackPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
return base.MeasureOverride(availableSize);
} protected override Size ArrangeOverride(Size finalSize)
{
return base.ArrangeOverride(finalSize);
}
}
}
Step 3: Measure
A layout pass is made up of two steps. Measuring is the first. In the measure step, each control determines how much space it needs. Panels do this by measuring the child controls, and then basing it's desired size on the child controls. Content controls base their size on their content. Each panel tells its children how much space is available, and each child tells its parent how much space it wants.
Lets examine how this works by writing an example. Here is the code for MeasureOverride.
protected override Size MeasureOverride(Size availableSize)
{
double sumX = 0.0;
double maxY = 0.0;
foreach (UIElement child in this.Children)
{
child.Measure(new Size(Math.Max(availableSize.Width - sumX, 0.0), availableSize.Height));
sumX += child.DesiredSize.Width;
maxY = Math.Max(maxY, child.DesiredSize.Height);
}
return new Size(sumX, maxY);
}
Step 4: Arrange
The second step in the layout pass is arranging. In the arrange step, each control arranges its content or children based on the available space. Each panel tells its children how much space they have been given. Each child tells the parent how much space it actually used. You may ask why the measure and arrange passes aren't combined. Often a control is given more or less space than it asked for in the measure pass, and then it will arrange itself differently.
To see how this works, let's write the ArrangeOverride method.
protected override Size ArrangeOverride(Size finalSize)
{
double x = 0.0;
for (int i = ; i < this.Children.Count - ; i++)
{
UIElement child = this.Children[i];
child.Arrange(new Rect(x, 0.0, child.DesiredSize.Width, child.DesiredSize.Height));
x += child.DesiredSize.Width;
}
if (this.Children.Count > )
{
UIElement lastChild = this.Children[this.Children.Count - ];
lastChild.Arrange(new Rect(x, 0.0, Math.Max(finalSize.Width - x, 0.0), lastChild.DesiredSize.Height));
}
return finalSize;
}
Step 5: Using the panel
I used this code to test the panel. Feel free to use it with other code, but I can't claim that it will work in all scenarios. It was just designed as an example. Anyway, here is MainWindow.xaml.
<Window x:Class="WpfLayoutSystem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfLayoutSystem"
Title="MainWindow" Height="" Width="">
<local:ExpandingStackPanel>
<Button>A Button</Button>
<Ellipse Width="" Height="" Stroke="Black" />
<TextBlock>Some</TextBlock>
<RadioButton>Other</RadioButton>
<TextBox>Controls</TextBox>
<Button>Another Button</Button>
</local:ExpandingStackPanel>
</Window>
For your convenience, here is the entire ExpandingStackPanel class.
using System;
using System.Windows;
using System.Windows.Controls; namespace WpfLayoutSystem
{
class ExpandingStackPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
double sumX = 0.0;
double maxY = 0.0;
foreach (UIElement child in this.Children)
{
child.Measure(new Size(Math.Max(availableSize.Width - sumX, 0.0), availableSize.Height));
sumX += child.DesiredSize.Width;
maxY = Math.Max(maxY, child.DesiredSize.Height);
}
return new Size(sumX, maxY);
} protected override Size ArrangeOverride(Size finalSize)
{
double x = 0.0;
for (int i = ; i < this.Children.Count - ; i++)
{
UIElement child = this.Children[i];
child.Arrange(new Rect(x, 0.0, child.DesiredSize.Width, child.DesiredSize.Height));
x += child.DesiredSize.Width;
}
if (this.Children.Count > )
{
UIElement lastChild = this.Children[this.Children.Count - ];
lastChild.Arrange(new Rect(x, 0.0, Math.Max(finalSize.Width - x, 0.0), lastChild.DesiredSize.Height));
}
return finalSize;
}
}
}
Understanding the WPF Layout System的更多相关文章
- WPF Layout 系统概述——Measure
原文:WPF Layout 系统概述--Measure 前言 在WPF/Silverlight当中,如果已经存在的Element无法满足你特殊的需求,你可能想自定义Element,那么就有可能会面临重 ...
- WPF Layout 系统概述——Arrange
原文:WPF Layout 系统概述--Arrange Arrange过程概述 普通基类属性对Arrange过程的影响 我们知道Measure过程是在确定DesiredSize的大小,以便Arrang ...
- 【翻译】Sencha Touch2.4 The Layout System 布局
[翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...
- 在Winform或WPF中System.Diagnostics.Process.Start的妙用
原文:在Winform或WPF中System.Diagnostics.Process.Start的妙用 我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑 ...
- WPF中System.Diagnostics.Process.Start的妙用
我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...
- WPF 类型“System.ComponentModel.ISupportInitialize”在未被引用的程序集中定义。
问题:类型“System.ComponentModel.ISupportInitialize”在未被引用的程序集中定义.必须添加对程序集“System, Version=4.0.0.0, Cultur ...
- WPF的System.Windows.Threading.DispatcherTimer的使用(每隔一定的时间重复做某事)
这里使用了一个进度条来展示, 前段代码: <Window x:Class="TimerTest.MainWindow" xmlns="http://schemas. ...
- WPF Layout 系统概述 MeasureOverride和ArrangeOverride
说的非常的好:多参考!!! https://blog.csdn.net/nncrystal/article/details/47416339 https://www.cnblogs.com/dingl ...
- WPF使用System.Windows.SystemParameters类获得屏幕分辨率
转自 http://hi.baidu.com/shirley_cst/item/a55c290c8aa2ee2ca0312da3 示例代码如下所示. double x = SystemParamete ...
随机推荐
- 3ds max删除了对象后,还是将原来所有对象输出的原因
原因是场景中除了 几何体 外还有 图形,如下图 将这些图形删除,几何体就都正常输出了.
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- Xcode因为证书问题经常报的那些错
去开始做 iOS开发的时候,因为证书问题 Xcode 经常报这样或那样的错,经过实践,现在看见 Xcode 报错已经心平气和了,经常报的错就那么多,整理一下. 1. 确认下证书是不是开发证书,如果是发 ...
- CSS3–1.css3 新增选择器
1.后代级别选择器 2.同辈级别选择器 3.伪类选择器 4.属性选择器 5.UI伪类选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...
- Pyqt QComboBox 省市区县联动效果
在Qt中, QComboBox方法窗口组件允许用户从列表清单中选择,在web中就是select标签,下拉选项. 省市区县的联动就是currentIndexChanged 获取当前的Index,通过这个 ...
- python 之socket 网络编程
socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Un ...
- C++ 内联函数笔记
要使用内联函数,必须采取下述措施之一: +在函数声明前加上关键字inline: +在函数定义前加上关键字inline. 通常的做法是省略原型,将整个定义(即函数头和所有函数代码)放在本应提供原型的地方 ...
- Python 遍历文件,字符串操作
写一个简单的脚本,循环遍历单层文件夹,检查源代码中是否有一些特殊的类. import os import codecs dirroot = "......" line_num = ...
- ImageSwitcher自定意效果+定时切换图片
Activity实现 1 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; ...
- hdu 5291 dp+优化 ****
多校实在高能 题解链接 题意:有n中糖果,每种糖果有ai个.分给A,B两个人.两人的糖果要一样多,可以都是0,1......m个.同一种糖果没有区别. 问有几种分法. 定义dp[i]表示两人之间相差i ...