更改DataGrid CheckBox列的背景以获取验证错误

时间:2013-08-20 20:56:06

标签: wpf validation checkbox datagrid

(Visual Studio 2010)我正在尝试在WPF数据网格中的CheckBoxColumn上执行验证。我已经设置了验证规则,如果它失败了,我希望单元格背景变为红色并有工具提示:

 <DataGridCheckBoxColumn Header="Checked" >
                <DataGridCheckBoxColumn.Binding>
                    <Binding Path="CheckProperty" Mode="TwoWay" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged" >
                        <Binding.ValidationRules>
                            <local:CheckValidationRule ValidationStep="UpdatedValue" />
                        </Binding.ValidationRules>
                    </Binding>
                </DataGridCheckBoxColumn.Binding>
                <DataGridCheckBoxColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <Trigger Property="Validation.HasError" Value="True">
                                <Setter Property="Background" Value="Red"/>
                                <Setter Property="ToolTipService.ToolTip" 
                              Value="{Binding RelativeSource={RelativeSource Self},
                                Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>

但是当验证失败时,我的单元格样式将被完全忽略。我实际上已经习惯了这个(但我从未想过为什么被忽略了),所以我尝试使用TemplateColumnCheckbox,我可以改变CheckBox边框背景,但仍未弄清楚如何更改整个单元格的背景颜色。

1 个答案:

答案 0 :(得分:0)

尝试使用DataTrigger而不是Trigger Property:

<DataGridCheckBoxColumn Header="Checked" Binding="{Binding CheckProperty}">
    <DataGridCheckBoxColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=CheckProperty}" Value="True">
                    <Setter Property="Background" Value="White"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=CheckProperty}" Value="False">
                    <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>