如何将 RowValidationErrorTemplate 动态添加到 DataGrid?
How to dynamically add RowValidationErrorTemplate to a DataGrid?
我想使用 C# 代码将 RowValidationErrorTemplate 添加到 DataGrid(即不在 XAML 中)。
对应的XAML:
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid Margin="0,-2,0,-2"
ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
如果您想知道这背后的原因,这是我的情况:
- 我有几个继承自 .cs 代码的 UserControls。
- 每个 UserControl 包含一个 DataGrid,它具有:RowValidationErrorTemplate、事件处理程序、验证方法、 ...等等
我将 EventHandlers 移动到基础 class,现在我正在寻找一种方法将验证代码的最后一部分移动到基础 class。
您可以使用 XamlReader.Parse
方法动态创建 ControlTemplate
:
string xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><Grid Margin=\"0,-2,0,-2\" ToolTip=\"{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}\"><Ellipse StrokeThickness=\"0\" Fill=\"Red\" Width=\"{TemplateBinding FontSize}\" Height=\"{TemplateBinding FontSize}\" /><TextBlock Text=\"!\" FontSize=\"{TemplateBinding FontSize}\" FontWeight=\"Bold\" Foreground=\"White\" HorizontalAlignment=\"Center\" /></Grid></ControlTemplate>";
dataGrid.RowValidationErrorTemplate = System.Windows.Markup.XamlReader.Parse(xaml) as ControlTemplate;
我想使用 C# 代码将 RowValidationErrorTemplate 添加到 DataGrid(即不在 XAML 中)。 对应的XAML:
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid Margin="0,-2,0,-2"
ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
如果您想知道这背后的原因,这是我的情况:
- 我有几个继承自 .cs 代码的 UserControls。
- 每个 UserControl 包含一个 DataGrid,它具有:RowValidationErrorTemplate、事件处理程序、验证方法、 ...等等
我将 EventHandlers 移动到基础 class,现在我正在寻找一种方法将验证代码的最后一部分移动到基础 class。
您可以使用 XamlReader.Parse
方法动态创建 ControlTemplate
:
string xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><Grid Margin=\"0,-2,0,-2\" ToolTip=\"{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}\"><Ellipse StrokeThickness=\"0\" Fill=\"Red\" Width=\"{TemplateBinding FontSize}\" Height=\"{TemplateBinding FontSize}\" /><TextBlock Text=\"!\" FontSize=\"{TemplateBinding FontSize}\" FontWeight=\"Bold\" Foreground=\"White\" HorizontalAlignment=\"Center\" /></Grid></ControlTemplate>";
dataGrid.RowValidationErrorTemplate = System.Windows.Markup.XamlReader.Parse(xaml) as ControlTemplate;