Styles in WPF

In this article you will learn styles to a control in WPF.

Apply Styles to a control

Let's say you have created some button controls on a Form. It may be difficult to set styles to every control. To rescue this type of problem, we can set Styles to all controls by setting <styles> element.

Let's create buttons as follows:

Button Styles

Style attribute:

TargetType:

This is used to set Styles to a WPF controls as follows:

TagetType="Button" TargetType="Label" TargetType="ListBoxItem"

e.g.:

  1. <Style TargetType="Button">  
  2. </Style>  
Setting Style Properties to a control: This can be done by using property and value attributes.

e.g.:
  1. <Setter Property="FontWeight" Value="Bold"></Setter> Styles have to set in  
  2. <Window.Resources> </Window.Resources> Element.  
  3. <Window x:Class="WpfComboBox.ButtonStyles" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ButtonStyles" Height="300" Width="500">  
  4.     <Window.Resources>  
  5.         <Style TargetType="Button">  
  6.         <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="Foreground" Value="Red"></Setter> <Setter Property="Background" Value="LightSteelBlue"></Setter> <Setter Property="BorderBrush" Value="Pink"></Setter>  
  7.         </Style>  
  8.     </Window.Resources>  
  9.     <Grid>  
  10.         <StackPanel>  
  11.             <StackPanel Orientation="Horizontal" Background="Bisque">  
  12.                 <Button Content="Submit" Width="90" Height="35" Margin="20"></Button>  
  13.                 <ButtonContent="Cancel" Width="90" Height="35">  
  14.                     </Button>  
  15.             </StackPanel>  
  16.         </StackPanel>  
  17.     </Grid>  
  18. </Window>  
When you run the Program the Applied styles to a Button control looks as in the following image:

Button Styles

Thanks for reading my article.

Shakeer