How use styles In Silverlight 3 Application

No.of Views680
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  05 Apr 2010 10:04:33
Tag : Silver Light and XAML , Skin And GUI
In this article we will see how can we write simple styles for any control and use it effectively.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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

Creating Silverlight Project

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

 

Image Loading

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:

 

Image Loading

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.

 

Image Loading

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

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

 

Image Loading

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

 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    The color of the silverlight scroll bar was looking very ugly in one of my projects and it needs to be changed. Am so sure that the regular, default pale scroll bars suck most of the time. When the situation demands color tweaking for the scroll bar: Few simple steps and a few minutes of time, it’s done.
    Published Date : 15/Dec/2010
    Tilt an image on mouse move in Silverlight 3 (3D Animation)
    Published Date : 16/Feb/2010
    in this article, i will describe,How to use styles in Silverlight
    Published Date : 16/Feb/2010
    Every time you could have imagined for any background image for your application and for some controls. In Silverlight 2 and Silverlight 3 Beta it was not so easy to put Images as Background and do some Tiling. Using Tile Brush in Silverlight 3 we will be able to use images as background. In this article we will see how can we achieve that.
    Published Date : 08/Apr/2010
    Themes are nothing but some predefined styles for each and every control. For example you want a Blue Theme for your Silverlight Application that is for all of your controls inside the application.
    Published Date : 08/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top