WPF Databinding to existing objects

Nate Bross

Well-known member
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
Maybe this is a silly question, but I cant seem to find the answer.

How can I use WPF Databinding (Datacontext) to bind to an existing business object?

I have seen plenty of examples that impliment INotifyPropertyChanged, how can I databind if I cannot change these objects to impliment this interface?

TIA
 
Ideally it would be two way databinding.

Im looking for anything to get me started using my existing objects.

As a side note for future projects, Id be interested in how I should be writing my business objects for future so that they will support WPF databinding.
 
From the MSDN Documentation it looks like I need to impliment Dependency Properties in order to achieve true databinding functionallity.

This is the sample code that is provided from MSDN:

C#:
public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
...
    );

public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

Do me this seems like double work, am I missing something? It looks like all that happens is the Dependency Property replaces the local variable I would have used, but instead now I must do extra converting in my get/set blocks.

Id appreciate others thoughts on this.
 
It does initially seem a bit more work, however in the long run WPF does provide a lot more functionality for you if you implement dependency properties. e.g. A designer using Expression could attach an animation that is triggered by your property changing or an animation could modify your property on completion...
 
So in general there is not a more elegant version of the code I posted from MSDN?

Is there any performance loss by doing things this way?
 
Maybe this should be a new thread, but what is the difference between these Dependency Properties and using INotifyPropertyChanged?
 
Back
Top