Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

c# - Binding WPF Datagrid cell background colour with trigger

I want the background colour of a WPF datagrid cell to change colour when the contents have been modified. Each cell has behind it a ViewModel object which contains the following properties - Value, OriginalValue and Modified. When the user edits the cell contents, this automatically triggers the Amount property via data binding. This property setter then checks it against the original value and sets the boolean Modified property to true or false respectively, notifies the bindings for those properties to update.

I have so far achieved a partial result with a Style on the ElementStyle property of the DataGridTextColumn as follows

<Style x:Key="DataGridTextStyle" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=MyViewModel.Modified}" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

This updates the text content background colour, but that is only a small area in the center of the cell. I want the entire cell to updates it's background colour, not just the textblock attribute.

Can I modify the above trigger to search upwards in the visual tree to find a parent DataGridCell and set the Background property on that, rather than setting the background colour of the current textblock only?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to set CellStyle to target DataGridCell instead of only TextBlock.

If you want this dataTrigger to be applied for all cells in your dataGrid, set style on DataGrid CellStyle otherwise you can do that on specific DataGridTextColumn CellStyle as well.

DataGrid

     <DataGrid>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyViewModel.Modified}"
                                 Value="True">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

DataGridTextColumn

     <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding MyViewModel.Modified}" 
                                         Value="True">
                                <Setter Property="Background" Value="Yellow"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...