之前做项目的时候需要实现这样一个功能。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. Linux设备驱动程序 之 get_free_page

    get_free_page 如果模块需要分配大块的内存,使用面向页的分配会有很多优点: 分配页面可使用下面的函数: unsigned long get_zeroed_page(gfp_t gfp_ma ...

  2. shell 變數

    echo $? 上个命令的退出状态,或函数的返回值. ref: http://c.biancheng.net/cpp/view/2739.html

  3. Android:动态库(.so)调试技巧

    一.反汇编定位crash ①查看crash log: 上图已标出crash发生在 libdeflicker_gpu.so 库中的 default_fail_func() 函数,但是 libdeflic ...

  4. features its own

    Gulp.js features its own built-in watch() method - no external plugin required ---- However, the Arn ...

  5. git服务器搭建---便签做备注

    今天,简单搭建了一下git服务器.发现一篇文章写的挺好的 http://www.cnblogs.com/trying/archive/2012/06/28/2863758.html 并简单和廖雪峰的结 ...

  6. QPixmap QImage 相互转化

    QPainter p(this); QPixmap pixmap; pixmap.load("E:\\参考文件\\image\\1.jpg"); //QPixmap->QIm ...

  7. iOS开发之—— 加密使用(MD5,base64,DES,AES)

    基本的单向加密算法: BASE64 严格地说,属于编码格式,而非加密算法 MD5(Message Digest algorithm 5,信息摘要算法)SHA(Secure Hash Algorithm ...

  8. iOS-MBProgressHUD框架使用(转)

    MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgressHUD,然后把两个MBProgressHUD.h和MBP ...

  9. js或者jquery直接下载网页上的图片代码

    1.jquery方式 使用jquery直接下载图片 function downloadImage(src) { var a = $("<a></a>").a ...

  10. C#7:什么是丢弃物以及如何使用它们

    转载 http://www.devsanon.com/c/using-discards-feature-of-c-7 假设您希望调用一个具有返回值并且也接受out变量的方法,但是您不希望使用将要返回的 ...