The Grand User
The Grand User

Reputation: 727

Edit wpf control template but use original styles

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>

Upvotes: 5

Views: 2513

Answers (2)

Chris W.
Chris W.

Reputation: 23280

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.

Upvotes: 1

Nicolas Dias
Nicolas Dias

Reputation: 659

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>

Upvotes: 1

Related Questions