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 自适应布局控件的更多相关文章

  1. 【WPF】布局控件总结

    <Canvas>:画布,默认不会自动裁减超出内容,即溢出的内容会显示在Canvas外面,这是因为默认 ClipToBounds="False":可设置ClipToBou ...

  2. WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系

    WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系: 1.Canvas/WrapPanel控件: 其子控件的HorizontalAlign ...

  3. WPF自学入门(二)WPF-XAML布局控件

    上一篇介绍了xaml基本知识,我们已经知道了WPF简单的语法.那么接下来,我们要认识一下WPF的布局容器.布局容器可以使控件按照分类显示,我们一起来看看WPF里面可以使用哪些布局容器用来布局. 在WP ...

  4. WPF布局控件常用属性介绍

    WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0)   概述:WPF布局控件都是派生自System.Windows ...

  5. wpf布局控件总结

    首先要认识到wpf所有的布局控件都继承自Panel类,Panel类又继承自其他类.继承关系如下: 一.StackPanel布局面板 1.该面板在单行或者单列中以堆栈的形式放置其子元素. 默认情况下,S ...

  6. 布局控件Grid

    XAML概述 Silverlight的控件绘制是由XAML语言进行支持的.什么是XAML语言? 简单的说,XAML(Extensible Application Markup Language )是一 ...

  7. Expression Blend实例中文教程(3) - 布局控件快速入门Grid

    上一篇对Blend 3开发界面进行了快速入门介绍,本篇将基于Blend 3介绍Silverlight控件.对于微软开发工具熟悉的朋友,相信您很快就熟悉Blend的开发界面和控件. XAML概述 Sil ...

  8. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

  9. 在WPF程序中将控件所呈现的内容保存成图像(转载)

    在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...

随机推荐

  1. c++中变量、变量名、变量地址、指针、引用等含义

    首先了解内存,内存就是一排房间,编号从0开始,0,1,2,3,4,5...... 房间里面一定要住人,新人住进去了,原来的人就走了:不管你住不住,里面都有人. 编号就是地址.里面的人就是内容,为了我们 ...

  2. JS程序的基本语法

    JS程序的基本语法 JS是区分大小写的.如:Name和name是两个变量 JS中每一条语句,一般以英文下的分号(;)结束.这个分号不是必须的.为了向PHP兼容,最好加上分号. 运算符和变量,以及操作之 ...

  3. socket远程执行命令

    两个脚本模拟远程执行命令 cmd_server.py import socket import subprocess # 运行系统命令 sk = socket.socket() addess = (' ...

  4. POJ 2318 /// 判断点与直线的位置关系

    题目大意: n块玩具箱隔板 m个玩具落地点 给定玩具箱的左上和右下两个端点 接下来给定n块隔板的上点的x和下点的x(因为y就是玩具箱的上下边缘) 接下来给定m个玩具落地点 输出n+1个区域各有的玩具数 ...

  5. java_缓冲流(字节输入流)

    /** * java.iko.BufferedInputStream extends InputStream * BufferedInputStream:字节缓冲输入流 * 构造方法: * Buffe ...

  6. springboot整合jpa和mybatis实现主从复制

    百度多方参考终于配出我自己的了,以下仅供参考 参考https://www.cnblogs.com/cjsblog/p/9712457.html 代码 首先数据源配置 spring.datasource ...

  7. 02.MyBatis在DAO层开发使用的Mapper动态代理方式

    在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...

  8. odoo中的QWeb模板引擎

    * 概述    QWeb是odoo主要模板引擎,采用xml表述,最后生成HTML文件    * 一般用法 #条件表达式 <t t-if="record.effort_estimate. ...

  9. 廖雪峰Java15JDBC编程-3JDBC接口-1JDBC简介

    JDBC:Java DataBase Connectivity Java程序访问数据库的标准接口 使用Java程序访问数据库的时候,Java代码并不是直接通过TCP连接去访问数据库,而是通过JDBC接 ...

  10. 「题解」:07.18NOIP模拟赛T1:星际旅行

    问题 A: 星际旅行 时间限制: 1 Sec  内存限制: 256 MB 题面 题面谢绝公开. 考试心路历程 拿到这道题感觉很懵逼,所以先搞的T2和T3,最后码了个暴力,结果还不如直接输出‘0’得分高 ...