RowValidationErrorTemplate 作为静态资源

Hui Liu-MSFT 46,961 信誉分 Microsoft 供应商
2024-05-15T08:38:10.3533333+00:00

我有:

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


 

我在同一项目中有许多 DataGrid 控件,我想通过应用资源 共享此模板以使用它,就像

 <DataGrid        RowValidationErrorTemplate="{StaticResource template1}" >

Note:此问题总结整理于:RowValidationErrorTemplate as a static resources

Windows Presentation Foundation
Windows Presentation Foundation
.NET Framework 的一部分,它提供统一的编程模型,用于在 Windows 上构建业务线桌面应用程序。
109 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 41,116 信誉分 Microsoft 供应商
    2024-05-15T09:43:18.9233333+00:00

    欢迎来到我们的 Microsoft 问答平台!

    可以将 ControlTemplate 放在 Window.Resources 中

        <Window.Resources>
            <ControlTemplate x:Key="template1">
                <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>
        </Window.Resources>
        <Grid >
            <DataGrid RowValidationErrorTemplate="{StaticResource template1}" >
    
            </DataGrid>
        </Grid>
    

    谢谢。


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助