虽然 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/