Posted in Silverlight

Silverlight 5.0 Released !!

Silverlight 5 is finally released!

In addition to the platform having a release, I was really pleased to see an update to the Silverlight Toolkit, which has been one of the most popular things almost every Silverlight developer/application uses.

I would love to explore this new set of tools and will post some sample codes as I build them..

Go download the stuff and start building great apps..

Have fun !!

Posted in C# 4.0, Silverlight

Dynamic Style/theme Change in Silverlight 4.0

In Silverlight application, we often need to change the theme or style dynamically. It is quite simple to do from code side. Here is an example how to do that.

I have two separate theme files, Theme1.xaml and Theme2.xaml defined as ResourceDictionary.

Theme1.xaml

  <ResourceDictionary  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
  xmlns:sys="clr-namespace:System;assembly=mscorlib">       
  <!-- explicit style (has a key) -->
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">
        <Setter Property="Text" Value="SuperAwesome Co. LLC" />
        <Setter Property="Foreground" Value="Orange" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="15" />
 </Style>
 <!-- implicit style (no key - just a TargetType) -->
 <Style TargetType="Button">
     <Setter Property="Foreground" Value="Orange" />
     <Setter Property="Background" Value="Yellow" />
</Style>
</ResourceDictionary>

Theme2.xaml

<ResourceDictionary     
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
   xmlns:sys="clr-namespace:System;assembly=mscorlib">     
   <!-- explicit style (has a key) -->     
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">         
     <Setter Property="Text" Value="SomeOtherCompany Inc." />         
     <Setter Property="Foreground" Value="CornflowerBlue" />        
     <Setter Property="FontWeight" Value="Bold" />         
     <Setter Property="FontSize" Value="14" />     
  </Style>       
  <!-- implicit style (no key - just a TargetType) -->     
  <Style TargetType="Button">         
     <Setter Property="Foreground" Value="BlueViolet" />         
     <Setter Property="Background" Value="DarkOrchid" />     
  </Style> 
</ResourceDictionary>

Now I have added their reference in my App.xaml as below;

 <Application.Resources>         
    <ResourceDictionary>             
       <ResourceDictionary.MergedDictionaries>                 
         <ResourceDictionary Source="Themes/theme1.xaml"/>                 
         <ResourceDictionary Source="Themes/theme2.xaml"/>             
       <ResourceDictionary.MergedDictionaries>         
   </ResourceDictionary>    
</Application.Resources>

By default it will use Theme2.xaml as it is the last defined style. Now we’ll change the theme and use Theme1.xaml. Here is the code to change the theme dynamically from code behind.

  ResourceDictionary rd = new ResourceDictionary();
  //Load resource dictionary
  rd.Source = new Uri("/Themes/theme1.xaml", UriKind.RelativeOrAbsolute);
  //Clear previous styles if any...
  App.Current.Resources.MergedDictionaries.Clear();
  //Add the loaded resource dictionary to the application merged dictionaries
  App.Current.Resources.MergedDictionaries.Add(rd);

Hope this help some one 🙂