Is it possible to apply WPF style for different types at the same time?

I want to create a style that I can apply to various types of controls. Something like that:

<ToolBar> <ToolBar.Resources> <Style TargetType="Control"> <Setter Property="Margin" Value="1"/> <Setter Property="Padding" Value="0"/> </Style> </ToolBar.Resources> <ComboBox .../> <Button .../> </ToolBar> 

And it should apply to ComboBox and Button . But this does not work, as I wrote here.

Is it possible somehow? To target only the ancestor of these classes, for example Control ? If not, what would be the best way to apply general settings to a set of controls?

+4
source share
2 answers

Update

See this discussion for an interesting approach.

See this question

The style being created is intended only for control, and not for elements that are produced from Control. When you do not set x: Key, you implicitly set x: Key in TargetType, therefore, if TargetType = "Control", then x: Key = "Control". I do not think there is any direct way to achieve this.

Your options

 <Style x:Key="ControlBaseStyle" TargetType="Control"> <Setter Property="Margin" Value="1"/> <Setter Property="Padding" Value="0"/> </Style> 

Aim all buttons and ComboBoxes e.g.

 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ControlBaseStyle}"/> <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource ControlBaseStyle}"/> 

or use the style directly on the control

 <Button Style="{StaticResource ControlBaseStyle}" ...> <ComboBox Style="{StaticResource ControlBaseStyle}" ...> 
+9
source

I do not believe that styles support inheritance in the usual form of programming. It seems your best bet is to do what Meleak offers in his first example. This would force each type of control to have the same basic style, but you can also expand the styles for each type if you need to.

0
source

All Articles