Managing Implicit styiles in ResourceDictionary with the help of BasedOn in Silverlight.


You have several different buttons that share the same control template and several style properties, but with a small change like the background and foreground color, for this small change it is not recommended to create different styles for each background, so the best way is to use the Based-on styles.Based-on styles are use full for cascading/inheriting styles.

So how can we do this ??

To create a based-on style, you simply need to include a reference to the base style in the derived style’s definition:  BasedOn=”{StaticResource BaseStyle}”

Example:
<!– Base style for all button controls –>
<Style x:Key=”CommonStyle”
TargetType=”Button”>
<Setter Property=”FontSize”
Value=”14″ />
<Setter Property=”FontFamily”
Value=”Verdana” />
<Setter Property=”Foreground”
Value=”DarkBlue” />
</Style>

<!– Base style for all Content controls –>
<Style x:Key=”ContentControlBaseStyle”
TargetType=”ContentControl”
BasedOn=”{StaticResource CommonStyle}”>
<Setter Property=”HorizontalContentAlignment”
Value=”Center” />
<Setter Property=”VerticalContentAlignment”
Value=”Center” />
</Style>

In the above style you can see that there is a commonstyle which contains common properties for all button controls, and if we have to add any special property
to any particular button then we can inherit the base style and add addition style like in the ContenControlBaseStyle in the above example.

Managing Style in ResourceDictionary by using Based-On Style:

we can make implicit styles by removing Key from style, but for some control’s style it won’t work, in this case we can use Based-on property. To do this
first define your control’s style in resourcedictionary and at the bottom of the style (after the closing tag of Style) add the Based on Tag, but don’t remove
x:Key property from the style.
Example:
let say we have a style for button:
<Style x:Key=”ButtonStyle1″
TargetType=”Button”>
<Setter Property=”FontSize”
Value=”14″ />
<Setter Property=”FontFamily”
Value=”Verdana” />
<Setter Property=”Foreground”
Value=”DarkBlue” />
</Style>
<Style TargetType=”Button” BasedOn=”{StaticResource ButtonStyle1}” />

that’s it now we have created implicit style with the help of Based-On , now all buttons of our project will get this style by default without applying any
Style property.The best practice to mange our styles in resource dictionary is to add all Based-on Tags at the bottom(after all styles) of the resource dictionary file
With this there is another advantage we can easily identify that which style is using for which control, and we can easily stop and play styles from single
location that is from based-on tag location,

Example:
If we want to stop dappling ButtonStyle1(from Above) instead of Commenting entire style simply comment <Style TargetType=”Button” BasedOn=”{StaticResource ButtonStyle1}” />
This is one of the good trick.

Advertisements

One thought on “Managing Implicit styiles in ResourceDictionary with the help of BasedOn in Silverlight.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s