Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 736
The Grand User Avatar asked Oct 30 '22 11:10

The Grand User


1 Answers

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.

like image 96
Chris W. Avatar answered Nov 15 '22 07:11

Chris W.