WPF笔记(2.9和2.10)——Layout

简介: 原文:WPF笔记(2.9和2.10)——Layout 2.9讲的是,如果内部设定超过容器大小,怎么办?StackPanel会裁剪越界部分DockPanel和Grid会智能判断,从而决定换行。 2.10 自定义布局容器自定义容器要实现两个方法MeasureOverride和ArrangeOverride,并保证遍历其下的所有子控件,使他们都执行Measure和Arrange方法。
原文: WPF笔记(2.9和2.10)——Layout

 2.9讲的是,如果内部设定超过容器大小,怎么办?
StackPanel会裁剪越界部分
DockPanel和Grid会智能判断,从而决定换行。

2.10 自定义布局容器
自定义容器要实现两个方法MeasureOverride和ArrangeOverride,并保证遍历其下的所有子控件,使他们都执行Measure和Arrange方法。

img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Windows.Controls;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Windows;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
namespace  CustomPanel  img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
public class DiagonalPanel : Panel img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
protected override Size MeasureOverride( Size availableSize ) img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
double totalWidth = 0;
img_33d02437d135341f0800e3d415312ae8.gif            
double totalHeight = 0;
img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
foreach( UIElement child in Children ) img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                child.Measure( 
new Size( double.PositiveInfinity,
img_33d02437d135341f0800e3d415312ae8.gif                                         
double.PositiveInfinity ) );
img_33d02437d135341f0800e3d415312ae8.gif                Size childSize 
= child.DesiredSize;
img_33d02437d135341f0800e3d415312ae8.gif                totalWidth 
+= childSize.Width;
img_33d02437d135341f0800e3d415312ae8.gif                totalHeight 
+= childSize.Height;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
return new Size( totalWidth, totalHeight );
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
protected override Size ArrangeOverride( Size finalSize ) img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            Point currentPosition 
= new Point( );
img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
foreach( UIElement child in Children ) img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                Rect childRect 
= new Rect( currentPosition, child.DesiredSize );
img_33d02437d135341f0800e3d415312ae8.gif                child.Arrange( childRect );
img_33d02437d135341f0800e3d415312ae8.gif                currentPosition.Offset( childRect.Width, childRect.Height );
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
return new Size( currentPosition.X, currentPosition.Y );
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

 

目录
相关文章
|
前端开发 C#
WPF中的 Layout To Layout
原文:WPF中的 Layout To Layout                     WPF中的 Layout To Layout                            周银辉 WPF的布局功能异常强大,当有时我们会有一些奇怪的需求:布局之间的切换。
710 0
|
C# .NET 开发框架
WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新
原文:WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新 1、xmlns Mapping URI的格式是 clr-namespace:[;assembly=] (1)如果自定义类和XAML处在同一个Assembly之中,只还需要提供clr-namespace值。
1399 0
|
C# Windows
WPF Layout 系统概述——Measure
原文:WPF Layout 系统概述——Measure 前言 在WPF/Silverlight当中,如果已经存在的Element无法满足你特殊的需求,你可能想自定义Element,那么就有可能会面临重写MeasureOverride和ArrangeOverride两个方法,而这两个方法是WPF/SL的Layout系统提供给用户的自定义接口,因此,理解Layout系统的工作机制,对自定义Element是非常有必要的。
1035 0
|
C# 容器
WPF笔记(1.2 Navigation导航)——Hello,WPF!
原文:WPF笔记(1.2 Navigation导航)——Hello,WPF!这一节是讲导航的。看了一遍,发现多不能实现,因为版本更新了,所以很多旧的语法不支持了,比如说,不再有NavigationApplication,仍然是Application,TextBlock容器的TextWrap属性改为TextingWrap,StartupUri指向"Page1.xaml"。
807 0
|
C# C++ Windows
WPF笔记(1.1 WPF基础)——Hello,WPF!
原文:WPF笔记(1.1 WPF基础)——Hello,WPF! Example 1-1. Minimal C# WPF application// MyApp.csusing System;using System.
974 0
|
C# 容器
WPF笔记(1.3 属性元素)——Hello,WPF!
原文:WPF笔记(1.3 属性元素)——Hello,WPF! 这一节中“属性元素”的概念可以用匪夷所思形容。1。WPF用标签元素实现对象建模,有两种:Control和Container,都用来装载内容和行为,前者如Button,后者如Window。
666 0
|
C# 前端开发
WPF笔记(1.4 布局)——Hello,WPF!
原文:WPF笔记(1.4 布局)——Hello,WPF! 这一节只是第2章的引子。布局要使用Panel控件,有四种Panel,如下:DockPanel,就是设置停靠位置布局模型。StackPanel,提供一个从左至右或从上至下放置内容的堆栈模型。
858 0