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
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor