将相同的样式应用于多个元素

ds3*_*345 5 wpf xaml styles

我是新来使用WPF和我尝试应用Style(例如,背景为TextBox,Button并且MenuItem应该是橙色).为此,我做了类似的事情:

<Style TargetType="TextBox" x:Key="sampleTextBox">
    <Setter Property="Margin" Value="2"/>
    <Setter Property="FontFamily" Value="Verdana"/>
    <Setter Property="FontSize" Value="11px"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                <GradientStop Color="#FFFFD190" Offset="0.2"/>
                <GradientStop Color="Orange" Offset="0.85"/>
                <GradientStop Color="#FFFFD190" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

并重复了targettype Button和目标菜单的相同代码.这工作绝对正常.但我想通过可能具有多个targettype值来最小化重复代码的数量.

如果可能,请告诉我.

谢谢.

yo *_*han 6

  <Window.Resources>
    <Style x:Key="sampleTextBox">
        <Setter Property="Control.FontFamily" Value="Verdana"/>
        <Setter Property="Control.FontSize" Value="11px"/>
        <Setter Property="Control.FontWeight" Value="Bold"/>
        <Setter Property="Control.Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                    <GradientStop Color="#FFFFD190" Offset="0.2"/>
                    <GradientStop Color="Orange" Offset="0.85"/>
                    <GradientStop Color="#FFFFD190" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<StackPanel>
    <TextBlock Text="This is a string and it should be wrapped." Style="{StaticResource sampleTextBox}"/>
    <TextBox Text="This is a string and it should be wrapped." Style="{StaticResource sampleTextBox}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)