如何为数据网格动态列创建数据触发器? [英] How to create data trigger for data grid dynamic columns?
本文介绍了如何为数据网格动态列创建数据触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要创建具有可配置列的数据网格。因为单元格颜色将根据数据绑定而改变。
这可以通过该列的数据触发器(xaml)来完成 - ''只有单元格背景颜色''。
但我需要从视图模型类(mvvm wpf)生成动态列。并且需要为相应的列生成数据触发器。
我在google上花了很多时间,但最终结果是null。
解决方案
嗨。试试这种方式:
< DataGridTextColumn 标题 = 主题 >
< DataGridTextColumn.ElementStyle >
< 样式 TargetType = {x:Type TextBox} >
< 设定器跨度> <跨度class =code-attribute> 属性 = 模板 >
< Setter.Value >
< ControlTemplate TargetType = {x:Type TextBox} >
< span class =code-keyword>< TextBlock 文本 = {TemplateBinding Text} / >
< / ControlTemplate >
< ; / Setter.Value >
< / Setter >
< Style.Triggers >
< DataTrigger 绑定 = {绑定YourFavoriteProperty} 值 = >
< Setter Property = 背景 值 = / >
< / DataTrigger >
< / Style.Triggers >
< / Style >
< < span class =code-leadattribute> / DataGridTextColumn.ElementStyle >
< / DataGridTextColumn >
如果这不起作用告诉我。
祝你好运
嗨。这完全不起作用。如果你完成它,请告诉我:
< < span class =code-leadattribute>样式 x:Key = myStyle TargetType = {x:Type DataGridCell} >
< Setter 属性 = 模板 >
< Setter.Value >
< ControlTemplate TargetType = {x:Type DataGridCell} >
< ContentPresenter >
< ContentPresenter.ContentTemplate >
< DataTemplate >
< TextBox 文本 = {Binding RelativeSource = {RelativeSource AncestorType = DataGridCell,Mode = FindAncestor},Path = Content} / >
< / DataTemplate > ;
< / ContentPresenter.ContentTemplate >
< / ContentPresenter >
< / ControlTemplate >
< / Setter.Value >
< / Setter >
< / Style > 跨度>
Need to create a data grid with configurable columns. In that the cell colors will change according to the data bind.
This can be done by data trigger of that column(xaml) - ''only the cell background color''.
But i need to generate the dynamic column from view model class(mvvm wpf). And need to generate data triggers for the corresponding columns.
I have spent lots of day on googling but the end result is null.
解决方案
Hi. try this way :
<DataGridTextColumn Header="Subject" > <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <TextBlock Text="{TemplateBinding Text}"/> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding YourFavoriteProperty}" Value=""> <Setter Property="Background" Value=""/> </DataTrigger> </Style.Triggers> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn>
if this doesn''t work tell me.
Good Luck
Hi.this doesn''t work completely. if you complete it, please tell me:
<Style x:Key="myStyle" TargetType="{x:Type DataGridCell}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <ContentPresenter > <ContentPresenter.ContentTemplate> <DataTemplate> <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell, Mode=FindAncestor}, Path=Content}"/> </DataTemplate> </ContentPresenter.ContentTemplate> </ContentPresenter> </ControlTemplate> </Setter.Value> </Setter> </Style>
这篇关于如何为数据网格动态列创建数据触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文