In this article we will see how can we write simple
styles for any control and use it effectively.Crating Silverlight
Fire up Visual Studio 2008 and create a Silverlight
Application. Name it as StylesInSL3.
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.
Grid tag type
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
To make it simple we will be setting a
Style for TextBlock. So go ahead and properties to the Style tag.
the Style and set the TargetType to TextBlock.
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:
Add a TextBlock
to the Grid and change the Style property.
See this is as simple as that. Now Press F5 and you can see
the output as the Style is applied to the TextBlock.
we will change the ForeGround Color to Red. Add another Setter tag and add the
Property and Value.
that you know how to add Style a control in xaml code behind, play with other
Properties and other Controls.