之前做项目的时候需要实现这样一个功能。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控件中某一列根据另一个文本列的值显示相应的模板控件的更多相关文章

  1. Excel-判断一个文本字符串中是否包含数字! 判断一个文本字符串是否是纯汉字!

    0.判断一个文本字符串中是否包含数字!/判断一个文本字符串是否是纯汉字! 公式=IF(LENB(A1)=2*LEN(A1),"都是汉字","含有非汉字字符") ...

  2. C# WPF DataGrid在Grid中自适应窗体大小

    XAML 中设置   例如 <DataGrid AutoGenerateColumns="False" Margin="6" Name="dgV ...

  3. ch8 CSS 3列(等高文本列)

    css 3可以创建等高文本列,通过column-count.column-width.column-gap属性实现.假设标记如下: <h1>Socrates</h1> < ...

  4. 服务器控件中使用<%#...>, JS和html控件中使用<%=...>

    //在服务器控件的属性中,需要用<%#...>来绑定其他控件的ID, 并且要在页面初始方法中,执行Page.DataBind(); <asp:ImageButton ID=" ...

  5. C# WPF DataGrid 隔行变色及内容居中对齐

    C# WPF DataGrid 隔行变色及内容居中对齐. dqzww NET学习0     先看效果: 前台XAML代码: <!--引入样式文件--> <Window.Resourc ...

  6. ASP.NET自定义控件组件开发 第五章 模板控件开发

    原文:ASP.NET自定义控件组件开发 第五章 模板控件开发 第五章 模板控件开发 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接 ...

  7. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  8. 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列

    最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...

  9. WPF DataGrid列设置为TextBox控件的相关绑定

    在wpf的DataGrid控件中,某一列的数据模板为TextBox控件的话,绑定Text="{Binding TxtSn, UpdateSourceTrigger=PropertyChang ...

随机推荐

  1. HDU 2243 考研路茫茫――单词情结 ——(AC自动机+矩阵快速幂)

    和前几天做的AC自动机类似. 思路简单但是代码200余行.. 假设solve_sub(i)表示长度为i的不含危险单词的总数. 最终答案为用总数(26^1+26^2+...+26^n)减去(solve_ ...

  2. POI的XWPFTable的方法总结

    1. void  addNewCol(): 为该表中的每一行添加一个新列 2. void addRow(XWPFTableRow row): 向表中添加新行 3. boolean addRow(XWP ...

  3. [MyBatis]浅谈如何实现事务处理

    要实现事务处理,就得从SqlSession中取出connection来,然后对connection采用setAutoCommit,commit,rollback等操作,最后的时候,不能像JDBC一样关 ...

  4. 转载:OutOfMemoryError系列(2): GC overhead limit exceeded

    这是本系列的第二篇文章, 相关文章列表: OutOfMemoryError系列(1): Java heap space OutOfMemoryError系列(2): GC overhead limit ...

  5. 《maven实战》笔记(2)----一个简单maven项目的搭建,测试和打包

    参照<maven实战>在本地创建对应的基本项目helloworld,在本地完成后项目结构如下: 可以看到maven项目的骨架:src/main/java(javaz主代码)src/test ...

  6. CSS3常用属性及效果汇总

    本文转载于<https://blog.csdn.net/lyznice/article/details/54575905> 一.2D效果属性 要使用这些属性,我们需要通过 transfor ...

  7. computer5 environment

    luo@luo-All-Series:~/MyFile/Anaconda3$ luo@luo-All-Series:~/MyFile/Anaconda3$ luo@luo-All-Series:~/M ...

  8. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_14-webpack研究-webpack-dev-server

    实现自动打包自动刷新浏览器 新建目录和页面看图 cnpm install webpack@3.6.0 webpack-dev-server@2.9.1 html-webpack-plugin@2.30 ...

  9. 参照UB单创建DN并过账

    *&---------------------------------------------------------------------* *& Form FRM_DN_POST ...

  10. Security命名空间配置

    http://www.mossle.com/docs/springsecurity3/html/ns-config.html Security命名空间配置 2.1. 介绍 从Spring-2.0开始可 ...