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

Posted on 2/19/2007 @ 12:52 AM in #Silverlight and WPF by | Feedback | 8981 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!!

Sound off but keep it civil:

Older comments..


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.


On 3/27/2009 4:13:49 PM Trey said ..
Would you be able to provide a 2way binding example in WPF using LINQ to Entities modeling, using their DataGrid control?

I have grid's ItemSource = Entities.Customers


(Entities = the model, and Customers = an entity modeled to the Customers table.)

In addition, I have the columns in XAML bound, and set to two way... they seem to function in terms of what the grid is doing, and displaying... edits, new rows, etc... but no changes are being committed to the db.

Any thoughts?