Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply style in WPF Controls?

Tags:

wpf

I am beginner on WPF and need your help.

Problem: I have 4 buttons on the form and need to apply 2 different style on pair of 2 buttons.

Is there any way we can achive this ?

please provide me sample if possible...

Thanks in advance...

like image 704
Amit Avatar asked May 27 '10 11:05

Amit


2 Answers

Use this Code For different styles for different buttons or any other

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         HorizontalAlignment="Left"
         VerticalAlignment="Top">
<Window.Resources>
    **<Style x:Key="a" TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="Verdana" />
        <Setter Property="FontSize" Value="50"/>
        <Setter Property="Background" Value="Indigo"/>
    </Style>
    <Style x:Key="b" TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="16"/>
    </Style>
    <Style x:Key="c" TargetType="{x:Type Button}">
        <Setter Property="FontFamily" Value="TimesNewRoman" />
        <Setter Property="FontSize" Value="50"/>
        <Setter Property="Background" Value="Green"/>
    </Style>
</Window.Resources>
<Grid>
    <TextBlock Margin="26,41,39,0" Style="{StaticResource a}" Height="100" VerticalAlignment="Top">TextBlock with Style1</TextBlock>
    <TextBlock Margin="26,77,39,0" Height="32" VerticalAlignment="Top">TextBlock with no Style</TextBlock>
    <TextBlock Margin="26,105,67,96" Style="{StaticResource b}">TextBlock with Style2</TextBlock>
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="26,170,-26,0">
        <Button Style="{StaticResource c}">
            <Bold >Styles</Bold></Button>
        <Button Style="{StaticResource c}">are</Button>
        <Button Style="{StaticResource c}">cool</Button>
    </StackPanel>
</Grid>

here i declaring the style for both textBlock and button.Use this one..

like image 122
Sankar Avatar answered Oct 19 '22 06:10

Sankar


You can define named styles and then assign them explicitly to any controls as you wish. Here is a primer for styling buttons: Getting Started with WPF : Button Control Part 2 – Basic Styling

And here is an example:

<Window x:Class="WpfButtonStyling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="250" Width="400">
    <Window.Resources>
        <Style x:Key="ButtonStyle1" 
               TargetType="{x:Type Button}">
            <Setter Property="Foreground"
                    Value="Red" />
            <Setter Property="Margin"
                    Value="10" />
        </Style>
        <Style x:Key="ButtonStyle2" 
               TargetType="{x:Type Button}">
            <Setter Property="Foreground"
                    Value="Blue" />
            <Setter Property="Margin"
                    Value="10" />
        </Style>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <Button x:Name="FirstButton"
                    Content="First!"
                    Style="{StaticResource ButtonStyle1}"/>
            <Button x:Name="SecondButton"
                    Content="Second"
                    Style="{StaticResource ButtonStyle2}" />
        </StackPanel>
    </Grid>
</Window>
like image 37
Ben Collier Avatar answered Oct 19 '22 04:10

Ben Collier