WPF DataGrid DataTrigger绑定DataContext属性

编程入门 行业动态 更新时间:2024-10-18 03:25:50
本文介绍了WPF DataGrid DataTrigger绑定DataContext属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 DataContext属性的DataTrigger绑定是什么? 我有一个这样绑定的DataGrid:

XAML:

code>< DataGrid x:Name =dataGridGrid.Row =4Grid.ColumnSpan =2 ItemsSource ={Binding} CellStyle ={StaticResource RowStateStyle }> < / DataGrid> code

在.cs中,网格绑定到一个DataTable,因此单元格的DataContext是DataRowView,其中包含作为属性行:

//包含大量行/列的DataTable dataGrid.DataContext = dataTable;

编辑:

参考ASh的解决方案,我编辑了风格,并将其置于不变和修改的触发器中:

< Style x:Key =RowStateStyleTargetType ={x:Type DataGridCell}> < Style.Triggers> < DataTrigger Binding ={Binding Path = DataContext.Row.RowState, RelativeSource = {RelativeSource Self}}Value =Unchanged> < Setter Property =ForegroundValue =Green/> < / DataTrigger> < DataTrigger Binding ={Binding Path = DataContext.Row.RowState, RelativeSource = {RelativeSource Self}}Value =Modified> < Setter Property =ForegroundValue =Yellow/> < / DataTrigger> < DataTrigger Binding ={Binding Path = Content.Text, RelativeSource = {RelativeSource Self}}Value =test> < Setter Property =ForegroundValue =Red/> < / DataTrigger> < /Style.Triggers> < / Style>

对Content.Text的触发效果非常好,不变也是如此。但是当我修改单元格(因此DataRowState = Modified)时,没有任何反应,颜色保持绿色。任何解决方案?

解决方案

DataTrigger适用于我,如果我

  • 使用 DataContext.Row.RowState 路径

  • 使用 Mode = TwoWay

  • 并删除枚举名称 DataRowState 当设置值

  • < DataTrigger Binding ={Binding Path = DataContext.Row.RowState, RelativeSource = {RelativeSource Self}} Value =Unchanged> < Setter Property =ForegroundValue =Red/> < / DataTrigger>

    我发现另一篇与此问题相关的帖子

    WpfToolkit DataGrid:突出显示已修改行

    没有通知关于 RowState 更改的默认机制。但是可以在派生的dataTable类中创建一个:

    public class DataTableExt:DataTable { protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {返回新的DataRowExt(builder); } protected override void OnRowChanged(DataRowChangeEventArgs e) { base.OnRowChanged(e); //行已更改,通知有关更改 var r = e.Row as DataRowExt; if(r!= null) r.OnRowStateChanged(); } }

    与派生的dataRow类:

    public class DataRowExt:DataRow,INotifyPropertyChanged { protected internal DataRowExt(DataRowBuilder builder):base(builder) {} internal void OnRowStateChanged() { OnPropertyChanged(RowState); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if(handler!= null)handler(this,new PropertyChangedEventArgs(propertyName)); } }

    What's the correct DataTrigger binding for DataContext properties? I have a DataGrid which is bound like this:

    XAML:

    <DataGrid x:Name="dataGrid" Grid.Row="4" Grid.ColumnSpan="2" ItemsSource="{Binding}" CellStyle="{StaticResource RowStateStyle}" > </DataGrid>

    In .cs the grid is bound to a DataTable, hence the DataContext for a cell is DataRowView, containing Row as a property:

    // DataTable containing lots of rows/columns dataGrid.DataContext = dataTable;

    Edit:

    Refering to ASh's solution I edited the style and put in Triggers for Unchanged and Modified:

    <Style x:Key="RowStateStyle" TargetType="{x:Type DataGridCell}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=DataContext.Row.RowState, RelativeSource={RelativeSource Self}}" Value="Unchanged"> <Setter Property="Foreground" Value="Green" /> </DataTrigger> <DataTrigger Binding="{Binding Path=DataContext.Row.RowState, RelativeSource={RelativeSource Self}}" Value="Modified"> <Setter Property="Foreground" Value="Yellow" /> </DataTrigger> <DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}" Value="test"> <Setter Property="Foreground" Value="Red" /> </DataTrigger> </Style.Triggers> </Style>

    Triggering on Content.Text works perfectly fine, so does Unchanged. However when I modify a cell (thus DataRowState = Modified), nothing happens and the color stays green. Any solution?

    解决方案

    DataTrigger works for me, if I

  • use DataContext.Row.RowState path

  • don't use Mode=TwoWay

  • and remove enum name DataRowState when set Value

  • <DataTrigger Binding="{Binding Path=DataContext.Row.RowState, RelativeSource={RelativeSource Self}}" Value="Unchanged"> <Setter Property="Foreground" Value="Red" /> </DataTrigger>

    I have found another post related to this issue

    WpfToolkit DataGrid: Highlight modified rows

    There is no default mechanism which notifies about RowState changes. But it is possible to create one in a derived dataTable class:

    public class DataTableExt: DataTable { protected override DataRow NewRowFromBuilder(DataRowBuilder builder) { return new DataRowExt(builder); } protected override void OnRowChanged(DataRowChangeEventArgs e) { base.OnRowChanged(e); // row has changed, notifying about changes var r = e.Row as DataRowExt; if (r!= null) r.OnRowStateChanged(); } }

    with a derived dataRow class:

    public class DataRowExt: DataRow, INotifyPropertyChanged { protected internal DataRowExt(DataRowBuilder builder) : base(builder) { } internal void OnRowStateChanged() { OnPropertyChanged("RowState"); } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } }

    更多推荐

    WPF DataGrid DataTrigger绑定DataContext属性

    本文发布于:2023-11-06 23:45:49,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1564994.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:绑定   属性   DataGrid   WPF   DataContext

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!