1
votes

I have a DataGrid with data binding to DataTable with columns set to auto generate.

The first column has boolean data type replaced with DataGridTemplateColumn with CheckBox from DataTemplate. It all works fine as it is.

However, now I want to make the DataGridCell background to red when the CheckBox is not checked.

The problem is, I have no idea how to set CheckBox's parent DataGridCell style with IsChecked trigger.

WPF:

    <Window.Resources>
    <DataGridTemplateColumn x:Key="colSelect">   

        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Name="chkBxSelect" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center"
                  IsChecked="{Binding Path=Select, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                  Click="chkBxSelect_Click">
                </CheckBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>

        <DataGridTemplateColumn.HeaderTemplate>
            <DataTemplate>
                <CheckBox x:Name="chkBxSelectAll" 
                    Content="Select" 
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center" 
                    IsThreeState="True" 
                    Click="chkBxSelectAll_Click"  
                    IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.SelectAll}">
                    </CheckBox>
            </DataTemplate>
        </DataGridTemplateColumn.HeaderTemplate>

        <DataGridTemplateColumn.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Foreground" Value="White"></Setter>
                        <Setter Property="Background" Value="DarkGray"></Setter>
                        <Setter Property="BorderBrush" Value="Red"></Setter>
                        <Setter Property="BorderThickness" Value="1"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGridTemplateColumn.CellStyle>
    </DataGridTemplateColumn>
</Window.Resources>

C# while DataGrid Column AutoGenerating:

    DataGridTemplateColumn col = (DataGridTemplateColumn)Resources["colSelect"];
    e.Column = col;
    e.Column.IsReadOnly = false;

Update: So far I have figured out it could be done using RelativeSource and AncestorType in Binding. However, still trying to make it work.

1

1 Answers

1
votes

Well after struggling a lot and not even trying the most obvious solution. I found it. It was relatively very simple and easy.

Just added DataTrigger to DataGridCell style and all done, WPF is magic.

    <DataGridTemplateColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Foreground" Value="White"></Setter>
                <Setter Property="Background" Value="DarkGray"></Setter>
                <Setter Property="BorderBrush" Value="Red"></Setter>
                <Setter Property="BorderThickness" Value="1"></Setter>
            </Trigger>
            <DataTrigger Binding="{Binding Path=Select, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGridTemplateColumn.CellStyle>