如何在WPF Datagrid中更改单元格背景色
问题描述:
当我选择一个单元格并在UI中单击红色按钮或蓝色按钮时,我想在WPF Datagrid应用程序中更改单元格背景颜色。
I want to change the cell background color in my WPF Datagrid application when I select a cell and click on "Red Button" or "Blue Button" in UI.
我是WPF Datagrid架构的初学者,非常感谢有人可以帮助我。 (我在应用程序中使用MVVM)
I'm a beginner to this WPF Datagrid architecture and really appreciate if someone can help me with this. (I'm using MVVM for my application)
答
尝试以下代码:
<DataGrid Name="SampleDataGrid"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Status" Binding="{Binding State}" Width="100">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="OrangeRed"/>
<Style.Triggers>
<Trigger Property="Text" Value="Completed">
<Setter Property="Background" Value="GreenYellow"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
输出:
截图