Monday, September 17, 2007

Writing code behind for generic.xaml

Have you ever thought of a method which allows you to write code behind for your generic.xaml ?

If you fed up searching for such an option,here is the solution with 3 simple steps...

  1. Add a file named generic.xaml.cs
    • Right click on themes folder and select new class
    • Name it as generic.xaml.cs
  2. Change the definition of the class inside generic.xaml.cs as
    partial class generic :ResourceDictionary //This class should inherit from ResourceDictionary
  3. Link this code behind file to Gereric.xaml by using x:Class

    //The class name should be with namespace

Now you can write code for your generic.xaml elements in the generic.xaml.cs file.

The same method can be applied if you want to write code behind for any other xaml resource file.

A sample is attached here


  1. This offers an easy way to get style resources stored in generic.xaml.

    MyControls.themes.generic objGeneric = new MyControls.themes.generic();

    Me.Style = objGeneric["myStyle1"];