I have multiple UserControl XAML files that use a similar structure. I want to remove this duplication and thought to use a Style that overrides the Template of the UserControl (and then use ContentPresenter for the custom part).
But apparently the Template of a UserControl can’t be overwritten.
How do I this the clean way? Derive from something else then UserControl?
<UserControl x:Class="Class1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<sdk:Label Grid.Row="0" Content="Title1" Style="{StaticResource Header1}" />
<Border Grid.Row="1">
...
</UserControl>
<UserControl x:Class="Class2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<sdk:Label Grid.Row="0" Content="Title2" Style="{StaticResource Header1}" />
<Border Grid.Row="1">
...
</UserControl>
You could define a custom control like this. (I’m not sure if you need to specify the Title separate from the content, but here it is just in case.)
Then define its template:
You could then define a default style, as shown here. (Alternatively, you could give it an x:Key and explicitly set the style each time you use MyControl. You could also just set the Template property each time you use MyControl, instead of specifying this Style at all.)
Then to use it: