仅将WPF样式应用于一个按钮

时间:2014-06-10 15:48:40

标签: wpf xaml button styles target

我有一个按钮的样式,但我有几个,并希望样式只应用于一个按钮。我该怎么办?

 <Style TargetType="{x:Type Button}">
    <Setter Property="Background" >
        <Setter.Value>
            <ImageBrush ImageSource="resources/main_play_normal.png"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" >
                <Setter.Value>
                    <ImageBrush ImageSource="resources/main_play_over.png"/>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

1 个答案:

答案 0 :(得分:11)

给它一把钥匙:

 <Style TargetType="{x:Type Button}" x:Key="AButtonStyle">
 </Style>

然后将其应用于Button:

<Button Style="{StaticResource AButtonStyle}" ... />

您还可以使用常规XAML语法设置属性,将样式应用于任何控件:

<Button>
    <Button.Style>
        <Style TargetType="{x:Type Button}">
        </Style>
    </Button.Style>
</Button>