WPF工具包DataGridCell风格DataTrigger [英] WPF Toolkit DataGridCell Style DataTrigger

查看:484
本文介绍了WPF工具包DataGridCell风格DataTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个单元格的颜色变为黄色,如果该值已在DataGrid被更新

I am trying to change the color of a cell to Yellow if the value has been updated in the DataGrid.

我的XAML:

<toolkit:DataGrid x:Name="TheGrid"
                  ItemsSource="{Binding}"
                  IsReadOnly="False"
                  CanUserAddRows="False"
                  CanUserResizeRows="False"
                  AutoGenerateColumns="False"
                  CanUserSortColumns="False"                             
                  SelectionUnit="CellOrRowHeader" 
                  EnableColumnVirtualization="True" 
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Auto">
    <toolkit:DataGrid.CellStyle>
        <Style TargetType="{x:Type toolkit:DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsDirty}" Value="True">
                    <Setter Property="Background" Value="Yellow"/>
                 </DataTrigger>
            </Style.Triggers>
        </Style>
    </toolkit:DataGrid.CellStyle>
</toolkit:DataGrid>



网格被绑定到阵列的列表(显示值的表样如Excel会) 。阵列中的每个值是一个包含IsDirty依赖属性的自定义对象。当值改变IsDirty属性被设置

The grid is bound to a List of arrays (displaying a table of values kind of like excel would). Each value in the array is a custom object that contains an IsDirty dependency property. The IsDirty property gets set when the value is changed.

当我运行这样的:


  • 在列更改值1 =整排的那张黄色

  • 在任何其他列更改值=什么也没发生

我只希望改变细胞去黄不管其列。你看到什么错我的XAML?

I want only the changed cell to go yellow no matter what column its in. Do you see anything wrong with my XAML?

推荐答案

发生这种情况的原因是因为的DataContext 设置为行级和不改变各 DataGridCell 。所以,当你绑定到 IsDirty 将其绑定到行级数据对象的属性,而不是单元级之一。

The reason this happens is because DataContext is set at row level and doesn't change for each DataGridCell. So when you bind to IsDirty it binds to the property of row-level data object, not cell-level one.

由于您的例子表明,你有的AutoGenerateColumns 设置为false,我假设你生成列自己有这样的事 DataGridTextColumn 绑定属性设置为绑定到实际值字段。若要单元格样式变成黄色,你不得不改变 CellStyle 每个的DataGridColumn 是这样的:

Since your example shows that you have AutoGenerateColumns set to false, I assume that you generate columns yourself have something like DataGridTextColumn with Binding property set to binding to actual value field. To get cell style changed to yellow you'd have to change CellStyle on each DataGridColumn like this:

foreach (var column in columns)
{
    var dataColumn =
        new DataGridTextColumn
            {
                Header = column.Caption,
                Binding = new Binding(column.FieldName),
                CellStyle = 
                new Style
                    {
                        TargetType = typeof (DataGridCell),
                        Triggers =
                            {
                                new DataTrigger
                                    {
                                        Binding = new Binding(column.FieldName + ".IsDirty"),
                                        Setters =
                                            {
                                                new Setter
                                                    {
                                                        Property = Control.BackgroundProperty,
                                                        Value = Brushes.Yellow,
                                                    }
                                            }
                                    }
                            }
                    }
            };
    _dataGrid.Columns.Add(dataColumn);
}

您可以通过改变实验的DataContext 使用 DataGridColumn.CellStyle 的每个细胞。也许只有这样,你就可以像你没有这样做的每一列单独做电池的绑定到IsDirty直接从电网级风格。但我并没有实际的数据模型,你必须进行测试。

You can experiment with changing DataContext of each cell using DataGridColumn.CellStyle. Perhaps only then you'll be able to bind cell's to 'IsDirty' directly from grid-level style like you do without doing it for each column individually. But I don't have actual data model you have to test this.

这篇关于WPF工具包DataGridCell风格DataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆