WPF 自适应布局控件
public class KDLayoutGroup : Grid
{
public double LabelWidth { get; set; } public double GetLabelWidth()
{
return LabelWidth;
} public void SetLabelWidth(double value)
{
if (this.Parent is KDLayoutControl)
{
double w = (this.Parent as KDLayoutControl).GetLableWidth();
if (w < value)
{
(this.Parent as KDLayoutControl).SetLabelWidth(value);
} for (int i = ; i < Children.Count; i++)
{ SetBatchLabelWidth(Children[i], value);
} }
} protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
if (Children.Count == this.ColumnDefinitions.Count)
return; for (int i = ; i < Children.Count; i++)
{
var column = new ColumnDefinition();
//column.Width = new GridLength(0,GridUnitType.Auto);
this.ColumnDefinitions.Add(column); Children[i].SetValue(Grid.ColumnProperty, i); SetBatchLabelWidthOther(Children[i]); } base.OnRenderSizeChanged(sizeInfo);
} private void SetBatchLabelWidth(UIElement el, double value)
{
if (el is KDLayoutItem)
{
double width = (el as KDLayoutItem).GetLabelWidht();
if (width < value)
{
(el as KDLayoutItem).SetLabelWidht(value);
}
}
else
{
if (el is Panel)
{
var cs = (el as Panel).Children;
for (int i = ; i < cs.Count; i++)
{
SetBatchLabelWidth(cs[i], value);
}
} }
} private void SetBatchLabelWidthOther(UIElement el)
{
if (el is KDLayoutItem)
{ double width = (el as KDLayoutItem).GetLabelWidht();
if (width > LabelWidth)
{
LabelWidth = width;
SetLabelWidth(width);
}
}
else
{
if (el is Panel)
{
var cs = (el as Panel).Children;
for (int i = ; i < cs.Count; i++)
{
SetBatchLabelWidthOther(cs[i]);
}
} }
}
}
public class KDLayoutControl : StackPanel
{ public double LabelWidth { get; set; }
public double GetLableWidth()
{
return LabelWidth;
} public void SetLabelWidth(double value)
{
LabelWidth = value; for (int i = ; i < Children.Count; i++)
{
if ((Children[i] as KDLayoutGroup).GetLabelWidth() < LabelWidth)
{
(Children[i] as KDLayoutGroup).SetLabelWidth(LabelWidth);
}
}
} protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
for (int i = ; i < Children.Count; i++)
{ if ((Children[i] as KDLayoutGroup).GetLabelWidth() > LabelWidth)
{
LabelWidth = (Children[i] as KDLayoutGroup).GetLabelWidth();
}
} SetLabelWidth(LabelWidth); base.OnRenderSizeChanged(sizeInfo);
}
}
<UserControl x:Class="PHMES.UI.Base.KDLayoutItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PHMES.UI.Base"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Template>
<ControlTemplate TargetType="local:KDLayoutItem">
<DockPanel>
<Label x:Name="lbl" VerticalAlignment="Center" VerticalContentAlignment="Center" Content="{TemplateBinding Label}" />
<ContentPresenter/>
</DockPanel>
</ControlTemplate>
</UserControl.Template>
</UserControl>
public partial class KDLayoutItem : UserControl
{
public KDLayoutItem()
{
InitializeComponent();
}
public object Label
{
get { return (object)GetValue(LabelProperty); }
set
{
SetValue(LabelProperty, value);
}
} // Using a DependencyProperty as the backing store for Label. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register("Label", typeof(object), typeof(KDLayoutItem), new PropertyMetadata(null)); public double GetLabelWidht()
{
return (this.Template.FindName("lbl",this) as Label).ActualWidth;
}
public void SetLabelWidht(double width)
{
(this.Template.FindName("lbl", this) as Label).SetValue(WidthProperty,width);
}
}
功能类似dev的layoutcontrol,layoutgroup,layoutitem.
用法如下:
<Window x:Class="AutoWidthTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AutoWidthTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<local:KDLayoutControl>
<local:KDLayoutGroup Height="50" VerticalAlignment="Top" Margin="3">
<local:KDLayoutItem Label="aaa">
<TextBox />
</local:KDLayoutItem>
<local:KDLayoutItem Label="bbb">
<TextBox />
</local:KDLayoutItem>
<local:KDLayoutItem Label="ccc">
<TextBox />
</local:KDLayoutItem>
</local:KDLayoutGroup>
<local:KDLayoutGroup Height="50" VerticalAlignment="Top" Margin="3">
<local:KDLayoutItem Label="number">
<TextBox />
</local:KDLayoutItem>
<local:KDLayoutItem Label="name">
<TextBox />
</local:KDLayoutItem>
<local:KDLayoutItem Label="age">
<TextBox />
</local:KDLayoutItem>
</local:KDLayoutGroup>
<local:KDLayoutGroup Height="50" VerticalAlignment="Top" Margin="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<local:KDLayoutItem Label="a">
<TextBox />
</local:KDLayoutItem>
<local:KDLayoutItem Label="b" Grid.Column="1" Grid.ColumnSpan="2">
<TextBox />
</local:KDLayoutItem>
</Grid>
</local:KDLayoutGroup>
</local:KDLayoutControl>
</Window>
不管label字段有多长,KDLayoutControl会设置容器内所有的label长度一致。

WPF 自适应布局控件的更多相关文章
- 【WPF】布局控件总结
<Canvas>:画布,默认不会自动裁减超出内容,即溢出的内容会显示在Canvas外面,这是因为默认 ClipToBounds="False":可设置ClipToBou ...
- WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系
WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系: 1.Canvas/WrapPanel控件: 其子控件的HorizontalAlign ...
- WPF自学入门(二)WPF-XAML布局控件
上一篇介绍了xaml基本知识,我们已经知道了WPF简单的语法.那么接下来,我们要认识一下WPF的布局容器.布局容器可以使控件按照分类显示,我们一起来看看WPF里面可以使用哪些布局容器用来布局. 在WP ...
- WPF布局控件常用属性介绍
WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0) 概述:WPF布局控件都是派生自System.Windows ...
- wpf布局控件总结
首先要认识到wpf所有的布局控件都继承自Panel类,Panel类又继承自其他类.继承关系如下: 一.StackPanel布局面板 1.该面板在单行或者单列中以堆栈的形式放置其子元素. 默认情况下,S ...
- 布局控件Grid
XAML概述 Silverlight的控件绘制是由XAML语言进行支持的.什么是XAML语言? 简单的说,XAML(Extensible Application Markup Language )是一 ...
- Expression Blend实例中文教程(3) - 布局控件快速入门Grid
上一篇对Blend 3开发界面进行了快速入门介绍,本篇将基于Blend 3介绍Silverlight控件.对于微软开发工具熟悉的朋友,相信您很快就熟悉Blend的开发界面和控件. XAML概述 Sil ...
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
- 在WPF程序中将控件所呈现的内容保存成图像(转载)
在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...
随机推荐
- namespace 命名空间
namespace作用:资源隔离 当我们不指定namespace时,默认放在default下 创建namespace kubectl create namespace 资源名称 在生产中,我们建议一个 ...
- Python对接支付宝支付自实现
Python对接支付宝支付自实现 # -*- coding: utf-8 -*- import base64 import json import urllib.parse from datetime ...
- Walk Through Squares HDU - 4758 AC自动机+简单状压DP
题意:给你两个串,求用m个R,n个D能组成多少个包含这两个串 题解:先构造一个AC自动机记录每个状态包含两个串的状态, 状态很容易定义 dp[i][j][k][status]表示在AC自动机K这个节点 ...
- 【主席树】 [CQOI2015]任务查询系统
模板题... 差分,然后用主席树维护时间点上的优先值和就好了 就是细节烦... #include<bits/stdc++.h> #define int long long #define ...
- Luogu P2717 寒假作业(平衡树)
P2717 寒假作业 题意 题目背景 \(zzs\)和\(zzy\)正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有\(n\)项寒假作业.\(zzy\)给每项寒假作业都定义了一个疲劳值 ...
- JAVA-第一课 环境的配置
首先 我们需要 下载java的开发工具包 jdk jdk 的下载地址::http://www.oracle.com/technetwork/java/javase/downloads/index.h ...
- HTML - 框架标签相关
<html> <head></head> <!-- frameset 框架标签 cols : 按照列进行区域的切分 rows : 按照行进行区域的切分 fra ...
- where方法的用法是ThinkPHP查询语言的精髓
where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询.表达式查询.快捷查询.区间查询.组合查询在内的查询操作.where方法 ...
- 前端基础之BOM与DOM操作
目录 BOM操作 navigator对象 screen对象 history对象 localtion对象 弹出框 计时 setTimeout() clearTimeout() setInterval() ...
- opencv3 使用glob遍历并修改文件名
1.函数说明 string::find()函数:是一个字符或字符串查找函数,该函数有唯一的返回类型,即string::size_type,即一个无符号整形类型,可能是整数也可能是长整数.如果查找成功, ...