0

I know I'm a pervert, but I am very curious, is there a way to make custom controls to seek for it's base class style first, and then it's own. Why I'm asking: I have some TreeView derived controls with custom item templates. I apply those templates, then I have a base style. Later I might apply some color palette. At the last step I have a problem. I need to apply 2 styles. PVStructuralTree is derived from TreeView it has some DependencyProperty DataTemplates that get inserted into resources in code.

PVStructuralTreeView

  • EmploeeTemplate
  • ... more templates

Default style for PVStructuralTreeView:

<Style x:Key="DefaultPVStructuralTreeView" TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultTreeView}">
    <Setter Property="EmploeeTemplate"><!-- This get inserted inro Resources in code -->
        <Setter.Value>
            <DataTemplate DataType="{x:Type s:Emploee}">
                ...
            </DataTemplate>
        </Setter.Value>
    </Setter>
    ... Lots of them here
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultPVStructuralTreeView}"/>

Default style for a TreeView (it's pretty big, so I won't post it here):

<Style TargetType="{x:Type TreeView}" BasedOn="{StaticResource DefaultTreeView}"/>

In color template.xaml file I'd like to have this + some magic to apply both styles at the same time (from Generic.xaml and themed one):

<Style x:Key="ThemedTreeView" TargetType="{x:Type TreeView}" BasedOn="{StaticResource DefaultTreeView}">
    ...
</Style>

<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource ThemedTreeView}"/>

But it just overwrites generic.xaml styles. I want it to add to it.

Now I'm doing this way:

<Style x:Key="ThemedPVStructuralTreeView" TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultPVStructuralTreeView}">
    ... CopyPaste from ThemedTreeView ...
</Style>

<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource ThemedPVStructuralTreeView}"/>

Does anyone knows the way how to reuse the ThemedTreeView style here?

zORg Alex
  • 372
  • 2
  • 15
  • What I wrote here overrides the Generic style completely. I want to build on it, not to override. I am copying ThemedTreeView style into PVStructuralTreeView's style in template.xaml, when I'd like to build on existing, not to copy the code. – zORg Alex Aug 09 '17 at 09:11
  • You can't base a Style on two other styles. – mm8 Aug 10 '17 at 09:04

1 Answers1

0

You can base a Style on Another (one only!) Style using the BasedOn property and override specific properties, but you cannot base a DataTemplate or a ControlTemplate on another template. This is not supported. A template must be defined as a whole:

WPF: Is there a way to override part of a ControlTemplate without redefining the whole style?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • You can't modify the template in a derived style. You can only set additional properties or "overwrite" ones set by the base style. So what is your current question? – mm8 Aug 09 '17 at 09:36
  • PVStructuralTree is derived from TreeView it has some DependencyProperty Templates that get inserted into resources in code-behind. – zORg Alex Aug 09 '17 at 09:42
  • Yes, I know about temlates. I was interested if there is a tricky way to base a style on two others. If not, then I'll stick to my CopyPaste way. – zORg Alex Aug 14 '17 at 06:38