Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF old Control Template inside new Control Template

Tags:

c#

wpf

I use a custom control from an external library, it has a property with ControlTemplate.

  <Style x:Key="{x:Type controls:HeaderDiscountButton}" TargetType="{x:Type controls:HeaderDiscountButton}" BasedOn="{StaticResource {x:Type core:ValueButton}}">
        <Setter Property="core:ButtonWithPopup.PopupTemplate">
            <Setter.Value>
                <ControlTemplate> 
                   some complicated xaml code
                </ControlTemplate>
          </Setter.Value>
        </Setter>
  </Style>

I would like to override it, but use old one in my new ControlTemplate, for example

  <Style x:Key="{x:Type controls:HeaderDiscountButton}" TargetType="{x:Type controls:HeaderDiscountButton}" BasedOn="{StaticResource {x:Type core:ValueButton}}">
        <Setter Property="core:ButtonWithPopup.PopupTemplate">
            <Setter.Value>
                <ControlTemplate> 
                   <StackPanel>
                      <Border>some complicated xaml code</Border>
                      <Border>some additional xaml code</Border>
                   </StackPanel>
                </ControlTemplate>
          </Setter.Value>
        </Setter>
  </Style>

I would like to avoid copying whole old code

like image 934
Dela Avatar asked Jan 26 '26 12:01

Dela


1 Answers

This is not possible I am afraid. You can indeed base a Style on another Style and override specfic setters but you cannot "override" or inherit only a part of a ControlTemplate. Unfortunately you must (re)define the entire template as a whole.

So in this case you actually need to copy the (whole) original template and then edit it as per your requirements.

like image 104
mm8 Avatar answered Jan 29 '26 07:01

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!