Click here to Skip to main content
15,881,741 members
Articles / Desktop Programming / XAML

Styles in Silverlight

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
14 Apr 2011CPOL2 min read 25.2K   2   3
How to create a Style and attach it to a control in Silverlight.

Introduction

We all are aware of CSS and user control concepts in ASP.NET which are used for styling web pages. A similar concept in Silverlight provides the option to use a predefined look for controls. While Styles are used for applying simple makeover to controls, Templates are used for complex styling with an option to change the complete visual representation. Generally, Styles are used for a consistent look throughout the application, and control templates are used for the visual improvement of controls.

In this post, we will create a style and will attach it to a control.

Defining a Style

Styles are set as common properties that can are reusable by various other instances. In Silverlight, styles are defined in a resource file and can be applied to a control against its Style property.

image

Now suppose we need a button style for our application. The first step we need to do is create a Style in the resource file. Here in this example, I will add a Style to the Application.Resource tag in the app.xaml file. The code below shows a style named MyCustomStyleButton with a TargetType of Button. The TargetType will ensure that this Style can be used for a specific type of control only.

Let's create a Style for a Button:

XML
<Application.Resources> 
<!–Creating a Button Style–> 
<Style x:Name="MyCustomeStyledButton" TargetType="Button"> 
</Style> 
</Application.Resources>

Now we will add properties to this Style using a Setter property collection.

XML
<Application.Resources> 
<!–Creating a Button Style–> 
<Style x:Name="MyCustomeStyledButton" TargetType="Button"> 
<!–Asigning properties with Setter Collection–> 
<Setter Property="Background" Value="AliceBlue"/> 
<Setter Property="Height" Value="28″/> 
<Setter Property="Width" Value="125″/> 
<Setter Property="VerticalAlignment" Value="Center"/> 
</Style> 
</Application.Resources>

Now for complex properties, we can define a Style as follows:

XML
<!–Complex Properties–> 
<Setter Property="BorderBrush"> 
<Setter.Value> 
<LinearGradientBrush EndPoint="1,0.5″ StartPoint="0,0.5″> 
<GradientStop Color="Red" Offset="0″ /> 
<GradientStop Color="#FF792525″ Offset="1″ /> 
<GradientStop Color="#FF351010″ Offset="0.439″ /> 
</LinearGradientBrush> 
</Setter.Value> 
</Setter>

So overall, the button style is ready to be use for button type controls.

XML
<Style x:Name="MyCustomeStyledButton" TargetType="Button" > 
<!–Asigning properties with Setter Collection–> 
<!–Simple Properties–> 
<Setter Property="Foreground" Value="Red"/> 
<Setter Property="Height" Value="28″/> 
<Setter Property="Width" Value="125″/> 
<Setter Property="VerticalAlignment" Value="Center"/> 
<Setter Property="FontWeight" Value="ExtraBold"/> 
<!–Complex Properties–> 
<Setter Property="BorderBrush"> 
<Setter.Value> 
<LinearGradientBrush EndPoint="1,0.5″ StartPoint="0,0.5″> 
<GradientStop Color="Red" Offset="0″ /> 
<GradientStop Color="#FF792525″ Offset="1″ /> 
<GradientStop Color="#FF351010″ Offset="0.439″ /> 
</LinearGradientBrush> 
</Setter.Value> 
</Setter> 
</Style>

Applying a Style to a Button

Now we need to attach the custom style to our button. which can be done by adding a static resource to the button style property.

XML
<Button Content="Submit" 
Margin="51,48,0,0″ 
Name="button1″ 
Style="{StaticResource MyCustomeStyledButton}" 
>

And our button will look like:

image

Explicit and Implicit Styling

The example above is an Explicit method of applying a Style to a control, where the controls are assigned with style names. But if we want the same button look throughout the application as a default style, then we need to follow the implicit way of styling.

For implicit styling, we will remove the name attribute from the XAML. So then the the style is applicable to all controls of the specified TargetType.

Implicit Style Definition
XML
<Style TargetType="Button" > 
<!–Asigning properties with Setter Collection–> 
<!–Simple Properties–> 
<Setter Property="Foreground" Value="Red"/> 
<Setter Property="Height" Value="28″/> 
<Setter Property="Width" Value="125″/> 
<Setter Property="VerticalAlignment" Value="Center"/> 
<Setter Property="FontWeight" Value="ExtraBold"/> 
<!–Complex Properties–> 
<Setter Property="BorderBrush"> 
<Setter.Value> 
<LinearGradientBrush EndPoint="1,0.5″ StartPoint="0,0.5″> 
<GradientStop Color="Red" Offset="0″ /> 
<GradientStop Color="#FF792525″ Offset="1″ /> 
<GradientStop Color="#FF351010″ Offset="0.439″ /> 
</LinearGradientBrush> 
</Setter.Value> 
</Setter> 
</Style> 

Conclusion

Styles help maintain consistency and also minimizes redundant code in XAML files tremendously. In our next post, we will see how to change the look of controls using control templates.

License

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


Written By
Microsoft
India India
Nothing special .. I like challenges and love my critics.

Microsoft | Bangalore | India

Blog : http://manaspatnaik.com/blog

Twitter@manas_patnaik

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mamta D12-Oct-11 6:32
Mamta D12-Oct-11 6:32 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»14-Apr-11 5:39
professionalKunal Chowdhury «IN»14-Apr-11 5:39 
GeneralRe: My vote of 5 Pin
Manas_Patnaik14-Apr-11 5:45
Manas_Patnaik14-Apr-11 5:45 

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.