RowValidationErrorTemplate as a static resources

essamce 621 Reputation points
2020-04-01T12:40:27.94+00:00

hi ,i have:

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


 

i have many datagrid controls in the same project and i want to share this template throught app resources
to use it like

 <DataGrid 
                  RowValidationErrorTemplate="{StaticResource template1}" >
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,669 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,096 Reputation points
    2020-04-02T03:41:25.727+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    You can put the ControlTemplate inside the 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>
    

    Thanks.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-04-01T15:02:05.23+00:00

    Hi, include your ControlTemplate in Application.xaml for app-wide using

    <Application x:Class="Application"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp1"
        StartupUri="Window1.xaml">
      <Application.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>
      </Application.Resources>
    </Application>
    
    1 person found this answer helpful.
    0 comments No comments