5

Sometimes when I'm editing a copy of the controls original template I don't need to change the original styles and colors, and would like to reference the original ones directly.

For example, I wanted to change the ComboBox template to add some filtering buttons in the drop down, its toggle button refers to a Style that is also copied into the file. I would like to refer to the original style so my XAML isn't overly cluttered.

Edit: So here is part of the XAML code that is created when you choose to edit a copy. The ControlTemplate is what I want to change, but I don't need the ComboBoxToggleButton Style, and so for the toggleButton I'd like to set its style to the one the ComboBoxToggleButton Style was copied from. Is there some namespace that they are all stored in, or are they inaccessible?

<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
     ...
</Style>

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
    <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
        ...
        <ToggleButton x:Name="toggleButton" ... Style="{StaticResource ResourceKey=ComboBoxToggleButton}"/>
     </Grid>
</ControlTemplate>

And approximately what I'd like it to be like

<Window xmlns:baseStyles="{namespace/url to the default wpf styles}">
<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
    <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
        ...
        <ToggleButton x:Name="toggleButton" ... Style="{StaticResource ResourceKey=baseStyles:ComboBoxToggleButton}"/>
     </Grid>
     <ControlTemplate.Triggers>
        ...
     </ControlTemplate.Triggers>
</ControlTemplate>
The Grand User
  • 727
  • 4
  • 14
  • Possible duplicate of [inherit style from default style](http://stackoverflow.com/questions/11581475/inherit-style-from-default-style) – Chris W. Mar 08 '17 at 22:52
  • No, because I'm not trying to inherit the style, I'm trying to remove the copied style entirely and just reference the one it was coped from. – The Grand User Mar 08 '17 at 23:07
  • See answer for more information based on your further explanation. – Chris W. Mar 09 '17 at 00:03

2 Answers2

1

To reuse the default WPF style to ComboBox, use:

<Style TargetType="ComboBox">
<!-- Setters in need of change -->
</ Style>

If you want to inherit from a Style you created yourself, you can use:

<Style TargetType="ComboBox" BasedOn="{StaticResource YourExistentStyle}">
<!-- Setters that need to change -->
</ Style>
Nicolas Dias
  • 641
  • 11
  • 22
1

Right, so Combobox isn't your basic bare templated control. Within it's ControlTemplate is a unique ToggleButton (hence the additional instance-specific Style template for it) which it requires. Once you introduce a new ControlTemplate than that's now all it knows. It CAN NOT reference a Style template inside of the original ControlTemplate since it's not a resource available outside of it. Style and ControlTemplate are different beasts.

You have two options. Either you take that unique ToggleButton Style Template and put it somewhere it can be reached as a StaticResource and ref it on the ToggleButton instance inside your ControlTemplate via the normal <ToggleButton Style="{StaticResource ComboBoxUniqueToggleButtonStyleKeyNameYouGiveIt}" ..../> (Like if it were in a resource dictionary, except then it's loaded all the time which generally isn't necessary).

Or, you can embed it directly in your ControlTemplate just like they do in the default style/controltemplate for ComboBox.

You can inherit parts of a Style template via BasedOn but you can only have one ControlTemplate at a time.

Hope this helps, and I'll retract my duplicate vote.

Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • So the regular ComboBox's ControlTemplate contains the style for the ToggleButton, even though when you edit a copy the it puts the Style outside the ControlTemplate? (I do note that that style has a ControlTemplate for the ToggleButton as well built into it) – The Grand User Mar 09 '17 at 01:14
  • Ya I can't speak to the logic of how VS extracts template parts. The Style template just provides a way of setting (Setter) property attributes of the ControlTemplate defined to it. You can apply those property settings to various ControlTemplate's via inheritance. So you could inherit the Style template (with it's ControlTemplate) of ToggleButton almost exactly like your second example. As long as it can reach a copy of that ToggleButton's Style template, otherwise it just goes to the default {x:Type ToggleButton} template and may not behave as you would like a ComoboBox ToggleButon to behave – Chris W. Mar 09 '17 at 20:46