question

essamce avatar image
essamce asked ·

RowValidationErrorTemplate as a static resources

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
AlexLi-MSFT answered ·

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.

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
PeterFleischer-3316 answered ·

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>
Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.