Wednesday, March 16, 2011

Binding to static classes in Windows Phone 7

It's a common request to be able to bind to a global variable in various parts of an application. It's often common that such variables are static.
This question seems to pop up in forums rather regularly and often the advice given is that this isn't possible or to include the "global" variable as part of the model being used as the DataContext of the page. Such duplication of functionality in the model is a bad idea and totally unnecessary.

It's not possible to bind to a static class as binding requires an object instance.
You can, however, bind to static properties of a class.

So we can bind to the static properties of the following.

namespace StaticBinding
  public class MyStaticClass
    private static string myStaticProperty = "my static text";

    public static string MyStaticProperty
      get { return myStaticProperty; }
      set { myStaticProperty = value; }

We can then create an application level resource which we can actually bind to:

.. xmlns:myns="clr-namespace:StaticBinding"


(Sorry, don't know why the syntax highlighter has forced upper case - I'm sure you're smart enough to work out what should and shouldn't be upper case though. ;)

With the above configured we can then bind to our global property in any page we wish. We just need to set the `Source` and `Path` of the binding.



A nice upside of this is that you even get intellisense on the `Path`. (Assuming you've set the `Source` first.)



  1. Anonymous1:51 am

    Love your site man keep up the good work

    generic nolvadex

  2. Anonymous8:04 am

    Depending on your age, you may or could possibly not bear in mind seeing your father wind his watch each and every evening before going to mattress. fake rolex sale If he did not, he would certainly wake to a watch that had stopped. louis vuitton replica sale Individuals days grew to become historical past with the advent of the automated watch. What makes it automated? cartier replica sale It still has the same standard mechanism to help keep the view operating, but how that mechanism is driven altered the way in which we cared for our watches. cartier replica All mechanical watches function in a similar manner. replica cartier sale They need a motion of the collection of gears to "tick" of increments of time, breitling replica sale which consequently registers as movements from the hands around the encounter in the watch.

  3. atlanta falcons,
    michael kors outlet online,
    mbt sneakers,
    dallas cowboys,
    oklahoma city thunder,
    san francisco 49ers,
    cheap uggs,
    polo ralph lauren uk,
    chicago bulls,
    babyliss hair dryer,
    supra sneakers
    ray ban sunglasses,
    toms shoes outlet online,
    snapback hats,
    fitflop sandals,
    louis vuitton outlet,louis vuitton outlet online,louis vuitton,,authentic louis vuitton,louis vuitton factory outlet,cheap louis vuitton
    ralph lauren sale,
    oakley outlet online,
    kkk10 13