user3678081
user3678081

Reputation: 65

apply WPF style to only one button

I have a style for buttons, but I have several of them and want the style to be applied only to one button. How can I do it?

 <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>

Upvotes: 2

Views: 5705

Answers (1)

McGarnagle
McGarnagle

Reputation: 102793

Give it a key:

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

And then apply it to the Button:

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

You can also apply a style to any control by using the normal XAML syntax for setting properties:

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

Upvotes: 14

Related Questions