Click here to Skip to main content
15,879,326 members
Articles / All Topics

Styles in WPF

Rate me:
Please Sign up or sign in to vote.
4.33/5 (4 votes)
11 Apr 2015CPOL4 min read 15.3K   5   2
The article will describe about the use of styles in WPF

Styles Introduction

Styles in WPF have been introduced to have a consistent look and feel of UI. E.g. all the buttons in our application should have same color, height , width and other properties. 
 
Style are objects which contains setters  objects (and trigger which I will dicuss in another article)collection which in turn can be used to assign the properties of a control. Each setter can be used to set the property which needs to be a dependency property .What a CSS in HTML does to control is same as what Style does to control in WPF.
We need to take care of following things while using styles –

1.       Setting a style or property locally will override the style defined above in the element tree.
2.       Styles can be set in the following two ways which I will discuss later
  • Named Style
  • Targeted Style
3.       Styles are decalred as resources  so that they can be used by the elements below them in element tree.

Named Style 

We can always declare a key for the style which can been used in the application down the element tree by specifying the key for the style.  An example of the named style in the resource is shown below.
                            
XML
 <Window.Resources>  
    <Style x:Key="buttonNamedStyle">  
        <Setter Property="Button.Height" Value="40"></Setter>  
        <Setter Property="Button.Width" Value="100"></Setter> 
        <Setter Property="Button.Background" Value="Red"></Setter>  
    </Style>     
</Window.Resources>  
 
I have defined a style for the button in the window resource. As we can see in the snippet I have provided a name for the style using x:key element. Futher I have defined values for number of styles using the Setter attribute. However we need to keep one point in mind while using named style is that while defining setter we need to provide the element name along with the propertyName in the property e.g. Button.Height as shown in the above code snippet. It doesn’t need to be the exact class name, it can also be the classs name from which the control derives e.g.  Control.Height which  in turn would help you to apply the property for any class down the Hierarchy ferived from that control. If some of the properties are relevant for the target control they would be simply ignored.  Now I will show how it can be used.
                      
  1. XML
    <Grid>  
         <Grid.ColumnDefinitions>  
              <ColumnDefinition></ColumnDefinition> 
            <ColumnDefinition></ColumnDefinition> 
         </Grid.ColumnDefinitions>   
         <Button Style="{StaticResource ResourceKey=buttonNamedStyle}" Content="MyButton" Grid.Column="0"></Button>  
         <Button Content="MyButton" Grid.Column="1"></Button>  
     </Grid>  



In the above code snippet I have used two button. One button explicitly uses the style definesd in resources section of the window and other one is not at all using any style. When we run the application we can see the difference between the two buttons as shown in the figure below.
Picture
 
Targeted Styles

This style is designed to provide a style for some particular type of control only and is applied automatically to the elements of the type below the element tree in the declaration. Below is the code snippet for the targeted style.
                               
  1. XML
    <Style TargetType="Button">  
        <Setter Property="Height" Value="40"></Setter> 
        <Setter Property="Width" Value="100"></Setter> 
        <Setter Property="Background" Value="Red"></Setter>    
    </Style> 
The changes which we can notice from that of the names style are as following.
1.  There is no x:Key.
2.  We have used TargetType attribute.
3.  In the setter we have removed the class name from Property attribute.

As soon as we apply this style in the resources section of the window both of the buttons which we have used in the previous section are changed as shown in the below figure, which shows that whatever elements come down in the element tree they take the style by default.
Picture
 
One more change I want to bring to your notice is that, if we are using targeted style in that case there is no need to use the Style attribute for the button as have used for the first button in Named Style.

Style Inheritance   

Styles in WPF also supports inheritane much like the inheritance in the OOP. This can be done using the BasedOn property that must point to other style to inherit from. If there is a target type specified in the base style it must be specified in the derived style also. Please check the below code snippet to get more about the BasedOn Property.
                        
  1. XML
    <Window.Resources>  
            <Style TargetType="Button" x:Key="mybaseStyle">  
                <Setter Property="Height" Value="40"></Setter> 
                <Setter Property="Width" Value="100"></Setter> 
                <Setter Property="Background" Value="Red"></Setter>   
            </Style>   
            <Style TargetType="Button" x:Key="derivedStyle" BasedOn="{StaticResource ResourceKey=mybaseStyle}">  
                <Setter Property="FontWeight" Value="ExtraBold" />  
                <Setter Property="Effect">  
                    <Setter.Value>  
                        <DropShadowEffect Color="Black" />  
                    </Setter.Value>  
                </Setter>  
            </Style>  
        </Window.Resources> 
And the output as shown in the figure below.
Picture
 
However it is not recommended to have more than one level of inheritance as it would be a nightmare to debug the changes if we have multiple levels.

Automatic Style

We can define a particular style for whole of the application for a single control by defining it in the App.xaml as shown below . These styles are created without key.                    
 
XML
 <Application.Resources>    
   <Style TargetType="Button">  
     <Setter Property="Height" Value="40"></Setter>  
     <Setter Property="Width" Value="100"></Setter> 
     <Setter Property="Background" Value="Red"></Setter>  
   </Style>          
</Application.Resources>   
 
If we want to revert the default style for the control we need to set the style as null (x:Null in xaml) or set the style to another named style. We can also create a style based on the application style as shown below.     
                      
XML
<Style TargetType="Button" x:Key="myButtonStyle"  BasedOn="{StaticResource {x:Type Button}}">   

The myButtonStyle in the example above derives from the automatic style and this is specified by looking up a resource with the key of typeof(Button), expressed in XAML as {x:Type Button}.
 
This article was originally posted at http://dotnetforyou.weebly.com/blog/styles-in-wpf

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I have 8 years of experience of mostly developing the .NET windows as well as web applications. Passionate about learning and sharing.

Visit my personal blog to know more about the .NET technology and increase your knowledge about C# and .NET. You can always follow me on twitter @dotnetforall.

Comments and Discussions

 
GeneralMy vote of 5 Pin
José G. Ramírez (Koaltares)13-Apr-15 10:27
José G. Ramírez (Koaltares)13-Apr-15 10:27 
Helpful article.
GeneralRe: My vote of 5 Pin
DotNetForAll13-Apr-15 18:24
DotNetForAll13-Apr-15 18:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.