wpf - 所有 DataGrid 的 DataGridTextColumn.EditingElementStyle

标签 wpf validation

虽然 MSDN 说 "you cannot use the Validation.ErrorTemplate attached property like you would with simple controls" 似乎令人困惑,我发现可以在我的每个窗口 xaml 中设置验证错误模板,如下所示:

<DataGridTextColumn ...>
    ...
    <DataGridTextColumn.EditingElementStyle>
        <Style>
            <Setter Property="Validation.ErrorTemplate" 
                    Value="{StaticResource MyValidationErrorTemplate}"/>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

我现在想要的是在共享主题资源字典中为所有数据网格设置错误模板。但我不能执行以下操作:
<Style TargetType="{x:Type DataGridTextColumn}">
    <Setter Property="Validation.ErrorTemplate" 
            Value="{StaticResource MyValidationErrorTemplate}"/>
</Style>

因为 DataGridTextColumn不是继承自 FrameworkElement ;因此不能有 style 属性(见 Why can't I style a DataGridTextColumn? )。

有人可以指出我正确的方向吗?

最佳答案

我正在寻找同一问题的答案。您可以使用以下样式使分配稍微不那么冗长。

<Style x:Key="TextColumnErrorStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplateStyle}"></Setter>
</Style>

用法:
<DataGridTextColumn Width="2*"  Header="IP Address" 
    Binding="{Binding IpDestination,    
    UpdateSourceTrigger=PropertyChanged, 
    ValidatesOnExceptions=True}"
    EditingElementStyle="{StaticResource TextColumnErrorStyle}"/>

啊。

关于wpf - 所有 DataGrid 的 DataGridTextColumn.EditingElementStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110333/

相关文章:

php - Zend form_element 添加带有自定义错误消息的验证器

java - Spring MVC validator : validate using current session object

wpf - 使用 Caliburn micro 的动态菜单

c# - 检测 IME(输入法编辑器)是否在 Silverlight 中处于事件状态

c# - 使用 URL WPF 打开浏览器

wpf - 在不使用 anchor 的情况下通过一条线连接两个 WPF Canvas 元素?

WPF - 当 ScrollViewer 滚动时设置图像可见性

javascript - 使用 React 进行表单验证

javascript - 单选按钮选择时显示 div 并设为必需

C++ 如何防止无效输入?