user2425286
user2425286

Reputation: 11

How to Call the Style function for button in wpf?

I have created button style in wpf form

<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
            <Button Background="Blue" FontStyle="Normal" 
            Padding="8,4" Margin="4" Foreground="White" FontWeight="Bold" Click="Button_Click">Prabhu
            </Button>
        </StackPanel>

I need to call this same style in 10 buttons. how to call the same style in all buttons?

Upvotes: 1

Views: 716

Answers (1)

Kevin Nacios
Kevin Nacios

Reputation: 2853

use the resources of whateve rtop level element you want to use:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="Button" x:Key="ButtonStyle">
            <Setter Property="Background" Value="Blue"></Setter>
            <Setter Property="FontStyle" Value="Normal"></Setter>
            <Setter Property="Padding" Value="8,4"></Setter>
            <Setter Property="Margin" Value="4"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="FontWeight" Value="Bold"></Setter>
        </Style>
    </StackPanel.Resources>

    <Button>Default Styled Button</Button>
    <Button Style="{StaticResource ButtonStyle}">Fancy Blue Button</Button>
</StackPanel>

if this is going to be a global style, or you want it accessible in your entire application, i recommend creating a resource dictionary and putting it in there. then in your app.xaml, reference the resource dictionary. this article goes more in depth on that topic

Upvotes: 1

Related Questions