且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF - 根据CheckBox值更改DataGridTemplateColumn单元格背景

更新时间:2022-04-04 02:57:02

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