Reuse Template from base style

Marius Herzog 21 Reputation points
2021-08-31T13:52:29.927+00:00

I have created a style for a window, so far so good:

<Style x:Key="windowStyle"
       TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Border Width="300" Height="300" BorderBrush="Red" BorderThickness="3">
                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now I want a specialized window for certain use cases. To this end I inherit from Window, add some dependency properties to my new class, etc. and create a style for it. In general I would like to keep the structure of my window from the base style, and just fill the Content with another Grid, where I want a particular cell to show the Window content:

<Style x:Key="customWindowStyle"
       BasedOn="{StaticResource windowStyle}"
       TargetType="{x:Type local:CustomWindow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Image Grid.Column="0" Source="{TemplateBinding Source}" />
                    <ContentPresenter Grid.Column="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Obviously however this overwrites the base template, so I would have to copy lots of XAML from the base style Template.
Is there an elegant solution for this?

I know I could separate the COntentPresenter part out and use DataTemplates for particular ViewModels, but I don't want to depend on a certain viewmodel and rather bind to dependency properties.

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,792 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
818 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,581 Reputation points Microsoft Vendor
    2021-09-06T10:00:03.127+00:00

    It is difficult to add and modify controls on this template without copying the existing template. It will be better to copy the entire template and modify it.
    Controls in Windows Presentation Foundation (WPF) have a ControlTemplate that contains the visual tree of that control. You can change the structure and appearance of a control by modifying the ControlTemplate of that control. There is no way to replace only part of the visual tree of a control; to change the visual tree of a control you must set the Template property of the control to its new and complete ControlTemplate.( From MSDN: http://msdn.microsoft.com/en-us/library/aa970773.aspx)


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [3]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.