SIGN UP MEMBER LOGIN:    
ARTICLE

Styles in Silverlight 3 Application

Posted by Diptimaya Patra Articles | XAML July 29, 2009
In this article we will see how can we write simple styles for any control and use it effectively.
Reader Level:
Download Files:
 

Introduction

In this article we will see how can we write simple styles for any control and use it effectively.

Crating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as StylesInSL3.

style1.gif

Open MainPage.xaml in design mode. In xaml code behind, you can see by default a grid is added to the User Control.

To make it simple I am going to put the styles inside the Grid, otherwise we can use User Control also.

Inside Grid tag type <Grid. And you will find the following:

style2.gif

You guessed it right; we can use User Control Resources also. But for the time being we wil stick with Grid.Resources.

Inside Grid.Resources insert a Style tag.

<Grid.Resources>
            <Style>

            </Style>
</
Grid.Resources>

To make it simple we will be setting a Style for TextBlock. So go ahead and properties to the Style tag.

Name the Style and set the TargetType to TextBlock.

<Style x:Name="MyTextBlockStyle" TargetType="TextBlock">

 </Style>

Now we will add a Setter. Setter is nothing but to set a specific property of the target control type to a value.

Suppose we want to change the FontSize of the TextBlock to value 32. So we need to write as follows:

<Setter Property="FontSize" Value="32" />

Add a TextBlock to the Grid and change the Style property.

<TextBlock Width="Auto" Height="Auto" Style="{StaticResource MyTextBlockStyle}" HorizontalAlignment="Center" Text="Styled Text" />

See this is as simple as that. Now Press F5 and you can see the output as the Style is applied to the TextBlock.

style3.gif

Now we will change the ForeGround Color to Red. Add another Setter tag and add the Property and Value.

<Setter Property="ForeGround" Value="Red"/>

style4.gif

Now that you know how to add Style a control in xaml code behind, play with other Properties and other Controls.

Enjoy Coding.

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor