C# 如何在DataGrid.RowValidationErrorTemplate中使用StaticResource?

C# 如何在DataGrid.RowValidationErrorTemplate中使用StaticResource?,c#,wpf,validation,C#,Wpf,Validation,在DataGrid中,我有RowValidationErrorTemplate,它工作得非常好。我的应用程序中有几个DataGrid,我想使用相同的ControlTemplate。我怎么做 <DataGrid.RowValidationErrorTemplate> <ControlTemplate> <Grid Margin="0,-2,0,-2"

在DataGrid中,我有RowValidationErrorTemplate,它工作得非常好。我的应用程序中有几个DataGrid,我想使用相同的ControlTemplate。我怎么做

        <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>

您应该在App.xaml或资源字典中的Window.Resources或Application.Resources中定义模板,给它一个x:Name,并将其应用到您想要的数据网格:

<Window....>
    <Window.Resources>
        <ControlTemplate x:Name="DataGridRowErrorTemplate">
            //Your template
        </ControlTemplate>
    </Window.Resources
</Window>

//你的模板
Add=>WPF=>ResourceDictionary,给它一个名称(例如MyDictionary),将模板放入其中,然后添加到App.xaml

<Application...>
    <Application.Resources>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    <Application.Resources>
</Application>

然后在DataGrid定义中,您只需执行以下操作:

<DataGrid RowValidationErrorTemplate={StaticResource DataGridRowErrorTemplate}>

</DataGrid>

谢谢@БМааааааааааааааааааа1072!两个小字体:
<DataGrid RowValidationErrorTemplate={StaticResource DataGridRowErrorTemplate}>

</DataGrid>