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 🙂

Advertisements
Posted in .Net Basics, C# 4.0

dynamic type in C# 4.0

The type dynamic was introduced with C# 4.0 long back but I have used this very recently in one of my project, so just want to share my experience with this wonderful feature of C# 4.0.

dynamic

The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. It behaves like type object in most circumstances, however the operations that contain expressions of type dynamic are not resolved or type checked by the compiler.

Variable of type dynamic is compiled into variable of type object, and that is why the dynamic type is exists at compile time but not at runtime.

How to use dynamic – a simple example:

class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

output:

System.Int32
System.Int32

you will see the output returns the same type as both the variables are having int value assigned. If you want to see the difference between dyn and obj at compile time, try following lines and compile the code.

dyn = dyn + 3;
obj = obj + 3;

The compiler raised an error for attempted addition of an integer and an object in expression obj + 3. But, no error is reported for dyn + 3. The expression that contains dyn is not checked at compile time because the type of dyn is dynamic.

For more information about the dynamic type http://msdn.microsoft.com/en-us/library/dd264741(VS.100).aspx and Using Type dynamic 

Posted in SQL Server

SQL Server Tips – Dynamic Order By

Many times we need to sort records based on some conditions and for that we need to implement conditional sorting in SQL server. I have mentioned some techniques to achieve this below.

Continue reading “SQL Server Tips – Dynamic Order By”