Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Style in WPF

Article:
Viewed:  1187 
Posted On:  19/09/2012 10:22:15 
Styles are like CSS in web application. WPF styles are like css in that they can be used to globally by defining once. 

To give a unique look to your control in WPF we have styles. Once we can define the look of a control and can apply this look in entire application no need to define again and again. In simple words we can say A style is a collection of property values that can be applied to an element. 

There are some properties of Styles Class in WPF

Resources
 â€“ is a ResourceDictionary where you can put objects used only within the Style, such as brushes, value converters, etc.

Setters
 â€“ a collection of Setter and EventSetter objects that apply values to properties, or assign handlers to events. 
              This is the 
content property of the Style class, which makes it very easy to use in XAML.

TargetType
 â€“ indicates on what type of element the Style will be applied, such as TreeView or Button.

In the below example I am showing a named style...

<Window x:Class="StylesInWPF.Window1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Style Demo in WPF" Height="300" Width="515">
    <Window.Resources>
        <Style x:Key="btnLogin" TargetType="Button">
            <Setter Property="FontStyle" Value="Italic" />
            <Setter Property="Padding" Value="8,4" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="Width" Value="125" />
            <Setter Property="Height" Value="40" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Top" />
            <Setter Property="FontFamily" Value="Arial" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="Background" Value="Blue" />
            <Setter Property="Margin" Value="5" />
        </Style> 

        <Style x:Key="btnJoin" TargetType="Button">
            <Setter Property="FontStyle" Value="Italic" />
            <Setter Property="Padding" Value="8,4" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="Width" Value="125" />
            <Setter Property="Height" Value="40" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Top" />
            <Setter Property="FontFamily" Value="Arial" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="Background" Value="Green" />
            <Setter Property="Margin" Value="5" />
        </Style> 

        <Style x:Key="btnAboutUs" TargetType="Button">
            <Setter Property="FontStyle" Value="Italic" />
            <Setter Property="Padding" Value="8,4" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="Width" Value="125" />
            <Setter Property="Height" Value="40" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Top" />
            <Setter Property="FontFamily" Value="Arial" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="Background" Value="Yellow" />
            <Setter Property="Margin" Value="5" />
        </Style>
    </Window.Resources> 

    <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
        <Button Style="{StaticResource btnLogin}">Login</Button>
        <Button Style="{StaticResource btnJoin}">Join</Button>
        <Button Style="{StaticResource btnAboutUs}">About Us</Button>
    </StackPanel>
</Window> 

In the above exampe I define 3 style for 3 button like..

StyleInWPF_1.png

Figure 1.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us