Site Logo

Pixel-in-Gene

Exploring Frontend Development with Design / Graphics / Technology

Creating an ObjectDataProvider using CLR properties

OK I agree that the title of this post doesn’t convey anything meaningful about the post ;) But the post itself is pretty useful, trust me.

I am sure most of you have heard and used the ObjectDataProvider (extensively). ObjectDataProvider (ODP) acts as a wrapper around your CLR object and helps in the data-binding. There are couple of ways of creating the ObjectDataProvider. Beatriz Costa did a great post on this topic, so I am not going over those details. Interestingly there is one more way of creating the ObjectDataProvider, which is just an extension of using the MethodName property.

The idea is to create the ODP by using the CLR property name. Lets see an example of where this is useful. Suppose I had a singleton class called ModelLocator.

public class ModelLocator
{
    private static ModelLocator _instance;
    private ModelLocator()
    {
    }

    public static ModelLocator GetInstance()
    {
        if (_instance == null)
        {
            _instance = new ModelLocator();
        }
        return _instance;
    }

}

I would be using the singleton instance of this class in all my data-bindings. But to use it in data-binding I need to have it as a resource in my XAML. Since ModelLocator is singleton, I cannot do something like this:

<local:ModelLocator x:Key="root"/>

The above statement doesn’t work because the constructor is private. So the next option is use the ObjectDataProvider and this can be done easily as:

<ObjectDataProvider x:Key="ModelRoot"
      ObjectType="{x:Type model:ModelLocator}"
      MethodName="GetInstance" />

This is good for XAML but in my C# code, I would have to reference the ModelLocator instance as:

ModelLocator model = ModelLocator.GetInstance();

What I would like instead, is a property called Instancethat returns me the ModelLocator instance.

public static ModelLocator Instance
{
  get
  {
    if (_instance == null)
    {
      _instance = new ModelLocator();
    }
    return _instance;
  }
}

But now creating the XAML resource for ModelLocator becomes a problem, because we are not going to keep the GetInstance() method. So how do we go about it. Well it so happens that you can use properties too for creating the ODP. Note that when the C# compiler compiles your properties, it actually generates a method whose name has a get_prefix (ex. get_Instance).

image

So all we have to do is provide get_Instanceas the value for the MethodNameproperty of ODP.

<ObjectDataProvider x:Key="ModelRoot"
      ObjectType="{x:Type model:ModelLocator}"
      MethodName="get_Instance" />

…and things work!

However I am a little skeptical about using it, primarily because I am relying on a low level detail of the C# compiler. But at the same time I think that the naming convention of get_prefix for property-methods may remain in future versions too. So it may be safe to use it.

I wonder what the good folks at Microsoft have to say about this.

Creating an ObjectDataProvider using CLR properties
Pavan Podila
Pavan Podila
February 15th, 2007