Willy
Willy

Reputation: 10650

Defining template and style together within the button

I have WPF button below:

<Button Name="btnAdd" Click="btnAdd_Click">                    
    <Button.Template>
        <ControlTemplate>                          
            <StackPanel Orientation="Vertical">
                <Image Height="20" Width="20" Stretch="UniformToFill" Source="./Resources/Add.png"/>                               
                <Label HorizontalAlignment="Center">Add</Label>
            </StackPanel>
        </ControlTemplate>                        
    </Button.Template>                     
</Button>

Now within the wpf button I am trying to add the following style, I mean, combining style with template in the button:

<Button>
    <Button.Style>
        <Style TargetType="Button" BasedOn="{StaticResource MyStyle}">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

How can I do this?

Upvotes: 0

Views: 29

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39966

One way could be adding a Setter to your Style for Template property. Like this:

<Button.Style>
    <Style TargetType="Button" BasedOn="{StaticResource MyStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel Orientation="Vertical">
                        <Image Height="20" Width="20" Stretch="UniformToFill" Source="./Resources/Add.png"/>
                        <Label HorizontalAlignment="Center">Add</Label>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Foreground" Value="Green" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>

Upvotes: 1

Related Questions