Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

WPF DataBinding & ADO.NET 3.5 Entities - made for each other!

Posted on 2/19/2007 @ 12:52 AM in #WPF | 1 comments | 5856 views

Okay, a complete blogpost is fully deserved here to the forethought and insight of the folks designing this stuff @ MSFT.

I just blogged about ADO.NET data binding with any object you wish using "DataSourceProvider". Earlier, I had also blogged about DataBinding with objects, in which I had mentioned that System.ComponentModel.INotifyPropertyChanged is the magic interface you need - if you want a changing object to be able to push changes to a databound UI, say a performance counter, a clock, a chat window etc.

Luckily, the ADO.NET Entities have this covered. Observe the following class diagram -

The "Category" class is an Entity in the NorthWindLib.Model.cs class, in the NorthWindLib project of the October CTP (which by the way works in the January CTP as well).

As you can see, the base class of Category is Entity, and the base class of Entity is StructuralObject, and StructuralObject does implement INotifyPropertyChanged.

StructuralObject also contains the following method -

protected virtual void ReportPropertyChanged(string property, object value)

{

    this.OnPropertyChanged(property);

}

Now, both "Entity" and "ComplexObject" (which inherit from StructuralObject) contain methods that look like this -

protected sealed override void ReportPropertyChanged(string property, object value)

{

    if (this._entityChangedHandler != null)

    {

        this._entityChangedHandler(this._cacheEntry, property, value);

    }

    base.ReportPropertyChanged(property, value);

}

 

AND

protected sealed override void ReportPropertyChanged(string property, object value)

{

    base.ReportPropertyChanged(property, value);

    if (this._entity != null)

    {

        this._entity.ReportComplexPropertyChanged(this._entityProperty, this);

    }

}

 

And if you see the auto generated code of the partial class, Category (or equivalent), you would see as an example -

[System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]

[System.Data.Objects.DataClasses.NullableAttribute(false)]

public string CategoryName

{

    get

    {

        return this._CategoryName;

    }

    set

    {

        this.ReportPropertyChanging("CategoryName", this._CategoryName);

        this._CategoryName = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false, 15, true);

        this.ReportPropertyChanged("CategoryName", this._CategoryName);

    }

}

So, in short, changing any property does have the ability to push the changes back to a WPF UI. So, ADO.NET Entity Framework objects, will happily databind with WPF UIs. I know this sounds trivial, but such use cases are very easy to ignore considering that you have to really imagine a world 2 years from today, when you will take this for granted.

I am glad this works!!


On 12/31/2007 11:25:29 AM Brent said ..
How would you do this same thing in Silverlight? It doesn't include System.ComponentModel.INotifyPropertyChanged in its library.

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.