WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件
之前做项目的时候需要实现这样一个功能。WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则"值"列显示下拉列表框控件。如果"更新状态"列的值为"序列号更新",则"值"列显示按钮控件。如果"更新状态"列的值为“文本值更新”,则"值"列显示文本控件。实现如下:
1、模型类
public class UpdateDataModel:INotifyPropertyChanged
{
public string Name { get; set; } public string UpdateStatus { get; set; } public string Value { get; set; } public List<string> ValueList { get; set; } public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
模型类
2、实现模板选择器
public class ValueSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
DataTemplate dt = new DataTemplate();
if (item != null && item is UpdateDataModel)
{
UpdateDataModel model = item as UpdateDataModel;
Window window = Application.Current.MainWindow;
if (model.UpdateStatus.Equals("固定值更新"))
{
//实例化下拉列表框控件
FrameworkElementFactory comboBox = new FrameworkElementFactory(typeof(ComboBox));
comboBox.SetBinding(ComboBox.ItemsSourceProperty, new Binding()
{
Path = new PropertyPath("ValueList"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
comboBox.SetValue(ComboBox.MarginProperty, new Thickness());
dt.VisualTree = comboBox; }
else if (model.UpdateStatus.Equals("序列号更新"))
{
//实例化按钮控件
FrameworkElementFactory button = new FrameworkElementFactory(typeof(Button));
button.SetBinding(Button.ContentProperty, new Binding()
{
Path = new PropertyPath("Value"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
button.SetValue(Button.ForegroundProperty, Brushes.Black);
button.SetValue(Button.HorizontalAlignmentProperty, HorizontalAlignment.Center);
//button.SetValue(Button.PaddingProperty, new Thickness(5, 0, 0, 0));
//button.SetValue(Button.BackgroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#218aff")));
dt.VisualTree = button;
}
else
{
//实例化文本控件
FrameworkElementFactory txtBox = new FrameworkElementFactory(typeof(TextBox));
txtBox.SetBinding(TextBox.TextProperty, new Binding()
{
Path = new PropertyPath("Value"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
txtBox.SetValue(TextBox.ForegroundProperty, Brushes.White);
txtBox.SetValue(TextBox.BackgroundProperty, new SolidColorBrush(Colors.Transparent));
dt.VisualTree = txtBox;
}
}
return dt;
}
}
模板选择器
3、界面
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="clr-namespace:WpfApplication1" Width="500" Height="350"
Title="MainWindow" Loaded="Window_Loaded">
<Window.Resources>
<u:ValueSelector x:Key="selector"></u:ValueSelector>
</Window.Resources>
<Grid>
<DataGrid Name="datagrid" Background="Transparent" Foreground="Black" SelectionMode="Single" BorderThickness="0"
AutoGenerateColumns="False" RowHeaderWidth="0" CanUserAddRows="False" AlternationCount="2" MinWidth="420"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" GridLinesVisibility="None" HeadersVisibility="Column">
<DataGrid.Columns>
<DataGridTextColumn Header="名称" Width="3*" Binding="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Header="更新状态" Width="3*" Binding="{Binding Path=UpdateStatus,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"></DataGridTextColumn>
<DataGridTemplateColumn Header="值" Width="4*" CellTemplateSelector="{StaticResource selector}" IsReadOnly="True"></DataGridTemplateColumn>
</DataGrid.Columns> </DataGrid>
</Grid>
</Window>
MainWindow.xaml
4、后台代码
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<string> ValueList = new List<string>() { "项1", "项2", "项3", "项4" };
List<UpdateDataModel> list = new List<UpdateDataModel>() {
new UpdateDataModel(){Name="测试数据1",UpdateStatus="固定值更新",ValueList=ValueList},
new UpdateDataModel(){Name="测试数据2",UpdateStatus="序列号更新",Value="设 置"},
new UpdateDataModel(){Name="测试数据3",UpdateStatus="文本更新"}
};
datagrid.ItemsSource = list;
}
}
MainWindow.xaml.cs
5、运行效果

示例代码:http://files.cnblogs.com/files/xiaomianyang/WpfApplication1.rar
WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件的更多相关文章
- Excel-判断一个文本字符串中是否包含数字! 判断一个文本字符串是否是纯汉字!
0.判断一个文本字符串中是否包含数字!/判断一个文本字符串是否是纯汉字! 公式=IF(LENB(A1)=2*LEN(A1),"都是汉字","含有非汉字字符") ...
- C# WPF DataGrid在Grid中自适应窗体大小
XAML 中设置 例如 <DataGrid AutoGenerateColumns="False" Margin="6" Name="dgV ...
- ch8 CSS 3列(等高文本列)
css 3可以创建等高文本列,通过column-count.column-width.column-gap属性实现.假设标记如下: <h1>Socrates</h1> < ...
- 服务器控件中使用<%#...>, JS和html控件中使用<%=...>
//在服务器控件的属性中,需要用<%#...>来绑定其他控件的ID, 并且要在页面初始方法中,执行Page.DataBind(); <asp:ImageButton ID=" ...
- C# WPF DataGrid 隔行变色及内容居中对齐
C# WPF DataGrid 隔行变色及内容居中对齐. dqzww NET学习0 先看效果: 前台XAML代码: <!--引入样式文件--> <Window.Resourc ...
- ASP.NET自定义控件组件开发 第五章 模板控件开发
原文:ASP.NET自定义控件组件开发 第五章 模板控件开发 第五章 模板控件开发 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接 ...
- 获取wpf datagrid当前被编辑单元格的内容
原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...
- 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列
最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...
- WPF DataGrid列设置为TextBox控件的相关绑定
在wpf的DataGrid控件中,某一列的数据模板为TextBox控件的话,绑定Text="{Binding TxtSn, UpdateSourceTrigger=PropertyChang ...
随机推荐
- JAVA基础知识|抽象类与接口类
一.抽象类 抽象类:拥有抽象方法的类就是抽象类,抽象类要使用abstract声明 抽象方法:没有方法体的方法,必须要使用abstract修饰 为什么要使用抽象类,抽象方法? 举例来说,如果你定义了一个 ...
- 走进JavaWeb技术世界2:JSP与Servlet的曾经与现在
转载自:码农翻身 转自: 刘欣 码农翻身 1周前 我是Servlet, 由于很多框架把我深深地隐藏了起来,我变得似乎无关紧要了,很多人也选择性的把我给遗忘了. 其实,我还活得好好的呢, 只不过是从前台 ...
- 彻底搞清楚javascript中的require、import和export(js模块加载规范的前世今生)
为什么有模块概念 理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. 但是,Javascript不是一种模块化编程语言,在es6以前,它是不支持”类”(class),所以也 ...
- 【NetDevops】网络自动化运维--1获取用户基本信息
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 之前博客的云主机到期了没续费,被删啦最重要的是没有备份!此处省略几个字..... ...
- unfortunately 遗憾的是
Yet,unfortunately,when it comes to the time for you to talk about these topics in English,......(unf ...
- .net framework 4.0 安装失败解决办法
方法一 1.打开cmd命令窗口 运行net stop WuAuServ 停止更新服务 2.开始----运行------输入%windir% 3.找到SoftwareDistribution的 ...
- BAPI_GOODSMVT_CREATE物料凭证增强字段
项目MSEG 的 BAPI 表增强结构 BAPI_TE_XMSEG 抬头MKPF 的 BAIP 表增强 BAPI_TE_XMKPF 1. 在结构BAPI_TE_XMSEG中appending str ...
- delphi DBTreeview与数据库连接的数据存取
//操作: procedure TForm1.act_GetListExecute(Sender: TObject); begin FieldList := TStringList.create; T ...
- MSMQ菜鸟教程
一 .MSMQ概述 MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于 ...
- 看看可爱c#中的delegate(委托)和event(事件)用法好不好
一.开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那.好了,那今天就趁月黑风高的夜晚简单来谈谈delegate和e ...