Change the wpf control template, but use the original styles

Sometimes, when I edit a copy of the original control template, I do not need to change the original styles and colors, and I would like to refer directly to the original ones.

For example, I wanted to modify the ComboBox template to add some filter buttons in the drop-down list, its switch button refers to a style that is also copied to the file. I would like to refer to the original style, so my XAML not too cluttered.

Edit: So here is the part of the XAML code that is created when you decide to edit the copy. The ControlTemplate is what I want to change, but I don't need the ComboBoxToggleButton style, so for toggleButton I would like to set its style to the one from which the ComboBoxToggleButton Style was copied. Is there any namespace in which they are all stored or 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 about what I would 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> 
+5
source share
2 answers

That's right, so Combobox is not your main template. Inside it, the ControlTemplate is a unique ToggleButton (hence, it has a special style template for a specific instance) that it requires. As soon as you submit a new ControlTemplate than now, all that he knows. It CANNOT reference the Style template inside the original ControlTemplate , as it is not a resource accessible outside of it. Style and ControlTemplate are different animals.

You have two options. Or you take this unique ToggleButton style template and place it somewhere, it can be reached as a StaticResource and reference the ToggleButton instance inside your ControlTemplate via the usual <ToggleButton Style="{StaticResource ComboBoxUniqueToggleButtonStyleKeyNameYouGiveIt}" ..../> it would be in the resource dictionary, except that it loaded all the time, which is usually not required).

Or you can embed it directly in your ControlTemplate in the same way as they do in the standard style style / controltemplate for ComboBox .

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

Hope this helps and I will return my re-vote.

+1
source

To reuse the default WPF ComboBox style, use:

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

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

 <Style TargetType="ComboBox" BasedOn="{StaticResource YourExistentStyle}"> <!-- Setters that need to change --> </ Style> 
+1
source

Source: https://habr.com/ru/post/1265200/


All Articles