WPF DataGrid DataTrigger绑定DataContext属性

时间:2016-06-26 16:46:29

标签: c# wpf xaml datagrid datatrigger

DataContext属性的正确DataTrigger绑定是什么? 我有一个像这样绑定的DataGrid:

XAML:

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

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

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

修改

参考ASh的解决方案,我编辑了样式并输入了触发器以保持不变和修改:

    <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>

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

1 个答案:

答案 0 :(得分:1)

DataTrigger适用于我,如果我

  1. 使用DataContext.Row.RowState路径

  2. 请勿使用Mode=TwoWay

  3. 并在设置值

  4. 时删除枚举名DataRowState
    <DataTrigger Binding="{Binding Path=DataContext.Row.RowState,
                 RelativeSource={RelativeSource Self}}" 
                 Value="Unchanged">
        <Setter Property="Foreground" Value="Red" />
    </DataTrigger>
    

    我找到了另一篇与此问题相关的帖子

    WpfToolkit DataGrid: Highlight modified rows

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

    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();
        }
    }
    

    使用派生的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));
        }
    }
    
相关问题