DataGridCell Validation.ErrorTemplate被忽略。

6
我正在尝试设置DataGridCells的Validation.ErrorTemplate,这里是xaml代码:
<Style x:Key="{x:Type DataGridCell}"  x:Uid="dataGridCellErrorTemplate" TargetType="{x:Type DataGridCell}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate >
                <Border BorderBrush="Green" BorderThickness="2" ToolTip="Heidenei"></Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- following line only for demonstration that the setter is working ... -->
    <Setter Property="Background" Value="Aquamarine"></Setter>              
</Style>

虽然数据网格单元格的背景颜色成功变为绿色(与任何验证结果无关),但使用的Validation.ErrorTemplate仍然是默认的红色边框。

我知道在stackoverflow上有类似的问题,例如Styling DataGridCell Error Template,但它们并没有真正解决我的问题。

感谢您的帮助。

Frank


由于编辑控件是在运行时创建的,因此您无法像简单控件一样使用Validation.ErrorTemplate附加属性。您可能需要在呈现后执行此操作(使用DispatcherTimer)。 - Jake Berger
谢谢您的反馈,但我不确定是否理解了它。在我的情况下,数据网格单元格不是由用户编辑的(因此没有编辑控件),而是由后台进程编辑的。我仍然希望使用IDataErrorInfo来突出显示那些具有问题值的字段。 - FrankE
当他们说“编辑控件”时,(我认为)他们指的是“嵌入在每个DataGridCell中的控件”。换句话说,模板可能会应用于单元格,但在创建内部控件时被覆盖。这就是为什么您可能需要捕获行创建的时间,并循环遍历每个单元格以设置自己的值的原因。 - Jake Berger
3个回答

7

我认为我也遇到了同样的问题。

当使用DataGridTemplateColumn时,内容会用ContentPresenter来呈现。该内容呈现器使用默认错误模板。

我找不到直接的方法来删除单个DataGridTemplateColumn的模板,但是您可以通过向DataGrid的资源添加样式来为所有内容呈现器删除该模板。

<DataGrid.Resources>
    <Style TargetType="ContentPresenter">
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    </Style>
</DataGrid.Resources>

1
真希望早点看到你的答案...我整个上午都在试图解决这个问题。我正在使用几个具有自定义验证模板的DataGridTemplateColumn,并且我需要删除单元格周围的默认红色边框。这个方法解决了我的问题。 - RogerN
1
非常感谢,找到解决方案并不容易! - olivier_sdg

1

我很幸运地通过使用以下的TextBlock样式成功去除了令人烦恼的红色边框。

<Style TargetType="TextBlock">
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Style>

0

来自微软,试图更改DataGrid上的ErrorTemplate是行不通的。他们的示例是在样式中设置触发器,当Validation.HasError属性为true时将背景设置为红色(或其他颜色)。

要自定义单元格验证反馈:将列的EditingElementStyle属性设置为适合该列编辑控件的样式。因为编辑控件是在运行时创建的,所以您不能像简单控件一样使用Validation.ErrorTemplate附加属性。

这种方法对我很有效,它还删除了默认的错误模板(单元格周围的红色边框)。红色边框被样式中的BorderBrush替换,可以通过将DataGrid的RowValidationErrorTemplate属性设置为{x:Null}来删除小感叹号。我查看了SO上的其他相关问题,但没有在任何地方找到这个解决方案。以下是我的解决方案示例,包括样式定义和使用该样式的DataGridTextColumn:

    <Style
        x:Key="DataGridTextBoxValidationStyle"
        TargetType="{x:Type TextBox}">
        <Setter Property="Padding" Value="-2" />
        <Setter Property="MaxWidth" Value="250"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=ActualWidth}"/>
        <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=ActualHeight}"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Firebrick" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                <Setter Property="BorderBrush" Value="Firebrick"/>
            </Trigger>
        </Style.Triggers>
    </Style>

<DataGrid RowValidationErrorTemplate="{x:Null}">
    <DataGrid.Columns>
        <DataGridTextColumn
            EditingElementStyle="{StaticResource DataGridTextBoxValidationStyle}">
             <!-- other stuff here -->
        </DataGridTextColumn>
   </DataGrid.Columns>
</DataGrid>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接