WPF - 根据 CheckBox 值更改 DataGridTemplateColumn 单元格背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17618077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Change DataGridTemplateColumn cell background based on CheckBox value
提问by user1017477
I need to change the background color of a DataGridTemplateColumn cell based on whether or not the CheckBox within the DataGridTemplateColumn is checked. Seems that this should be possible within xaml, how can I go about this?
我需要根据是否选中 DataGridTemplateColumn 中的 CheckBox 来更改 DataGridTemplateColumn 单元格的背景颜色。似乎这在 xaml 中应该是可能的,我该怎么做?
Column:
柱子:
<DataGridTemplateColumn Header="FSC-P" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding FSCP}"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I have seen this posthowever, this is not working for a TemplateColumn. Any help would be appreciated.
但是,我看过这篇文章,但这不适用于 TemplateColumn。任何帮助,将不胜感激。
回答by Richard E
The following Stylewill change the Backgroundcolor of the Cellif the CheckBoxis checked:
如果选中,以下Style将更改 的Background颜色:CellCheckBox
<Style x:Key="CheckBoxCellStyle" TargetType="DataGridCell">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox x:Name="cb"
IsChecked="{Binding FSCP, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding FSCP, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
<DataGridTemplateColumn Header="FSC-P" Width="SizeToHeader" CellStyle="{StaticResource CheckBoxCellStyle}"/>

