【问题标题】:DataGridCell Validation.ErrorTemplate ignoredDataGridCell Validation.ErrorTemplate 被忽略
【发布时间】:2012-03-15 14:34:49
【问题描述】:

我正在尝试设置 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>

虽然 datagridcells 的背景成功地被着色为绿色(独立于任何验证结果),但使用的 Validation.ErrorTemplate 仍然是默认的,即红色边框。

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

感谢任何帮助

弗兰克

【问题讨论】:

  • 感谢您的反馈,但我不确定我是否理解。在我的场景中,数据网格单元不是由用户编辑(因此没有编辑控件),而是由后台进程编辑。我还是想用 IDataErrorInfo 来突出那些值有问题的字段。
  • 当他们说“编辑控件”时,(我认为)他们的意思是“嵌入在每个 DataGridCell 中的控件”。换句话说,模板可能会应用于单元格,但在创建内部控件时会被覆盖。这就是为什么您可能必须在创建 Row 时捕获,并循环遍历每个单元格以设置您自己的值..

标签: wpf validation datagrid idataerrorinfo


【解决方案1】:

我相信我遇到了同样的问题。

使用DataGridTemplateColumn 时,内容以ContentPresenter 呈现。此内容演示者使用默认错误模板。

我找不到为个人DataGridTemplateColumn 删除此模板的直接方法,但您可以通过向 DataGrid 的资源添加样式来为 DataGrid 中的所有内容演示者删除它。

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

【讨论】:

  • 希望我能早点找到你的答案……花了整个上午试图弄清楚这个问题。我正在使用几个 DataGridTemplateColumn 和他们自己的自定义验证模板,我需要删除单元格周围的默认红色边框。这成功了。
  • 非常感谢,找到解决方案并不容易!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2016-06-05
    • 2014-02-25
    • 2011-05-09
    • 2015-02-08
    • 2011-03-27
    • 2010-11-19
    相关资源
    最近更新 更多