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 有的时候,我们需要将控 ...
随机推荐
- js加密数据爬取
- 中国空气质量在线监测分析平台是一个收录全国各大城市天气数据的网站,包括温度.湿度.PM 2.5.AQI 等数据,链接为:https://www.aqistudy.cn/html/city_deta ...
- java_序列化
import java.io.*; class People implements Serializable { /* * 序列化和反序列化的时候,会抛出就NotSerializableExcepti ...
- mysql commond record
CREATE DATABASE IF NOT EXISTS codex_gm DEFAULT CHARACTER SET utf8; service mysqld stop screen -dmS m ...
- html---三列布局
三列布局 1一 1.两边固定 当中自适应 2.当中列要完整显示 3.当中列要优先加载 <!DOCTYPE html> <html> <head> <meta ...
- 【JZOJ3301】家族
description 阿狸和桃子养了n 个小阿狸, 小阿狸们每天都在一起玩的很开心. 作为工程师的阿狸在对小阿狸们之间的关系进行研究以后发现了小阿狸的人际关系由某种神奇的相互作用决定, 阿狸称之为& ...
- thinkPHP 字段映射功能
thinkPHP的字段映射功能可以让你在表单中隐藏真正的数据表字段,而不用担心放弃自动创建表单对象的功能,假设我们的User表里面有username和email字段,我们需要映射成另外的字段,定义方式 ...
- vue 自定义指令input表单的数据验证
一.代码 <template> <div class="check" > <h3>{{msg}}</h3> <div clas ...
- Ubuntu 12.04 Eclipse设置 Javadoc背景色
在Ambiance主题下,eclipse弹出的tip是黑色背景的,这样压根就看不清java doc. 当然可以在外观改变系统主题为其他主题,相应的gtk-2.0/gtkrc要重新设置,比如Ubuntu ...
- <每日一题>题目28:简单的python练习题(51-60)
#51.一行代码实现1-100的和 sum(range(1,101)) #52.如何在一个函数内部修改全局变量 ''' 利用global ''' #53.字典如何删除和合并2个字典 ''' del d ...
- 关于排序--sort()和qsort()使用
一.sort()函数的使用 使用sort()函数的时候要加上头文件#include<algorithm>和using namespace std. 这个函数接收两个或者三个参数. 第一个参 ...