Friday, September 10, 2010

How many type of Styles in SilverLight

A Style is a collection of property values that you can apply to an element in one step. A style allows us to encapsulate control property values as a reusable resource. Silverlight supports only explicit styles. Styles are similar to CSS (Cascading Style Sheet) standard in HTML markup. We can set different properties for an element and store it in a name and then we can re-use it in our application. Before going to create a style, we should decide where to put it. We can store the Style in Resource property of a Control.

Every Style Element requires a Key, so that we can refer it anywhere using their Key. It supports only named types. It requires TargetType, which is the type of an element on which you r applying a style.

There are three ways to use Styles:

1. You can apply styles to particular page.
2. You can apply styles to particular Control.
3. You can apply styles globally.

Let us see all the steps one by one.

1. Applying Styles to a particular page:

Defining:

First we need to define the style in UserControl.Resource section like,

1.gif

A Setter element is a definition of one property to set on the target object. A Setter has a two Property one is Property naming the property to set on the target and another one is Value property defining the value to set on the property.

Here is your Page.xaml code:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">





Applying:

Then you can apply the style which you have created using the Style property of a Button as shown in the below code.

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">









3. Applying Styles Globally:

To use the Styles globally, i.e., you can use the Styles in any xaml pages throughout your application, for that you need to define the Style in Application.Resource in App.xaml file.

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="styling.App">






Here is your xaml file.

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">



Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates