如何在WPF Datagrid中更改单元格背景色 [英] How to Change Cell background color in WPF Datagrid
本文介绍了如何在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>
输出:
截图
这篇关于如何在WPF Datagrid中更改单元格背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文