简体   繁体   English

根据DataGridTemplateColumn中的CheckBox Checked状态设置DataGridCell样式

[英]Set DataGridCell style depending on CheckBox Checked state in DataGridTemplateColumn

I have a DataGrid with data binding to DataTable with columns set to auto generate. 我有一个DataGrid,数据绑定到DataTable,列设置为自动生成。

The first column has boolean data type replaced with DataGridTemplateColumn with CheckBox from DataTemplate. 第一列的布尔数据类型被DataTemplate的CheckBox替换为DataGridTemplateColumn。 It all works fine as it is. 一切正常。

However, now I want to make the DataGridCell background to red when the CheckBox is not checked. 但是,现在我想在未选中CheckBox时使DataGridCell背景变为红色。

The problem is, I have no idea how to set CheckBox's parent DataGridCell style with IsChecked trigger. 问题是,我不知道如何使用IsChecked触发器设置CheckBox的父DataGridCell样式。

WPF: 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: C#,同时自动生成DataGrid列:

    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. 更新:到目前为止,我已经知道可以使用Binding中的RelativeSourceAncestorType来完成。 However, still trying to make it work. 但是,仍在尝试使其工作。

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. 只需将DataTrigger添加到DataGridCell样式并完成所有操作,WPF就是魔术。

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM