是否可以同时将WPF样式应用于不同类型?

Meh*_*Meh 4 wpf inheritance xaml styles

我想创建一个可以应用于不同控件类型的样式.像这样的东西:

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

    <ComboBox .../>
    <Button .../>
</ToolBar>
Run Code Online (Sandbox Code Playgroud)

它应该适用于ComboBoxButton.但它不像我在这里写的那样有效.

这有可能吗?只针对这些类的祖先,比如Control?如果没有,那么将常用设置应用于一堆控件的最佳方法是什么?

Fre*_*lad 9

更新

请参阅讨论以获得有趣的方法

看到这个问题

您正在创建的样式仅针对Control,而不是从Control派生的元素.如果不设置x:Key,则隐式将x:Key设置为TargetType,因此如果TargetType ="Control",则x:Key ="Control".我认为没有任何直接的方法来实现这一目标.

你的选择是

<Style x:Key="ControlBaseStyle" TargetType="Control">  
    <Setter Property="Margin" Value="1"/>  
    <Setter Property="Padding" Value="0"/>  
</Style>  
Run Code Online (Sandbox Code Playgroud)

例如,定位所有按钮和组合框

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ControlBaseStyle}"/> 
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource ControlBaseStyle}"/> 
Run Code Online (Sandbox Code Playgroud)

或直接在Control上使用该样式

<Button Style="{StaticResource ControlBaseStyle}" ...>
<ComboBox Style="{StaticResource ControlBaseStyle}" ...>
Run Code Online (Sandbox Code Playgroud)