CheckBox值更改DataGridTemplateColum

CheckBox值更改DataGridTemplateColum

本文介绍了WPF - 根据CheckBox值更改DataGridTemplateColumn单元格背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据是否检查DataGridTemplateColumn中的CheckBox来更改DataGridTemplateColumn单元格的背景颜色。似乎这应该是可以在xaml,我该怎么办?

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 Header="FSC-P" Width="SizeToHeader">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding FSCP}"
                      VerticalAlignment="Center"
                      HorizontalAlignment="Center" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我看过这个但是,这不适用于TemplateColumn。任何帮助将不胜感激。

I have seen this post however, this is not working for a TemplateColumn. Any help would be appreciated.

推荐答案

以下 Style 将更改如果 CheckBox 被检查,背景颜色的单元格

The following Style will change the Background color of the Cell if the CheckBox is checked:

    <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}"/>

这篇关于WPF - 根据CheckBox值更改DataGridTemplateColumn单元格背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:25