Binding to a single object.

So today, on CodeProject, one of the WPF regulars posed a question about whether or not you could only bind to ObservableCollection objects, and if you could bind to single objects, how could you guarantee two way databinding goodness. As you may well imagine, I promptly answered that you could indeed bind to a single object and implement two way databinding with it. The discussion also posed the question, could WPF bind to properties that linked into child objects and display these – from separate usercontrols.

This seemed to me to be an ideal candidate for demonstrating the beauty and wonder that is the WPF databinding infrastructure. If you’ve only ever been exposed to WinForms or ASP.NET databinding, then you are in for a major treat – WPF (and Silverlight) provide so much more bang for your buck with binding that it’s hard to see how we managed before. First of all, let’s take a look at our business object classes, Benefit and Customer. Both of these classes implement INotifyPropertyChanged so that we get access to the notification mechanism that supports two way binding.

First of all, here’s Benefit.cs:

Benefit.cs

And here’s Customer.cs:

Customer.cs

As you can see, there’s nothing out of the ordinary in these classes – they don’t do anything clever. They are just your bog standard, basic model classes.

Now, here’s the main window XAML:

Window1.xaml

The TextBoxes are bound to the relevant entries in the Customer class using the familiar {Binding Path=…} syntax. But wait, where’s the Benefit class in all this? I added it for a reason, yet there’s no sign of it. Well, as you can see – there’s a reference to a UserControl (BenefitsControl), and we are going to perform the actual binding in this class. Here’s the definition of BenefitsControl.xaml:

benefitscontrol.xaml

Look – there’s the binding Benefits.ProviderName, but would it surprise you to know that I’m not going to put the data class anywhere near this control. I’m going to set the DataContext on the parent control and let the usercontrol “find” the data there. Here’s the implementation of the code behind the parent XAML (Window1.cs):

window1.cs

So – here’s the application in all its glory:

runningapplication

Well – now that we’ve covered the code, how does the databinding actually work? How does the user control actually get its binding to work? This is where the wonder that is WPF actually comes into play. If the binding can’t find data in the data context at the current level, it works its way back up the logical tree until it can. Seriously – I love this stuff, and I’d hate to go back from it.

The code for this application can be downloaded here. Note that you’ll need to change the extension from .doc to .zip.

customerboundsamplezip

17 thoughts on “Binding to a single object.

    1. peteohanlon

      Mea Culpa Rob – I shall put my hands up and plead guilty guvnor. Normally I’m fairly anal about putting a meaningful title, but wouldn’t you know I haven’t done this in the sample. Thanks for the link though.

    1. peteohanlon

      That’s odd. I’ve just checked them in IE and FireFox and they appear fine for me. Could you do a Ctrl-F5 and see if they appear fine.

  1. Hey Pete,

    Databinding is one of my favorite features in WPF/Silverlight too.

    My personal crusade for Blendability leads me to comment that the way you use for the creation of the Customer and Benefit objects will not work in Blend, and the data will not show up. Thankfully it is not very difficult to make this work in Blend: Simply use a factory class and place an instance of this class in the XAML resources. This way you get the best of both worlds, and the data will magically appear into the design surface in Blend 😉

    Cheers,
    Laurent

    1. peteohanlon

      Good call Laurent, however for the purpose of demonstrating simple binding to the guy on CodeProject, I wasn’t so worried about Blendability – but point taken, I will provide samples that do this in the future. To be honest, I would normally use MVVM for this type of object but I didn’t want to scare the original poster.

  2. Hey Pete,

    Totally understandable. The only reason I added this is not because I doubted you know this (we all know you do ;)) but rather to suggest/remind to readers that they can achieve blendability once they master the basics of databinding.

    Cheers 😉
    Laurent

    1. peteohanlon

      Laurent – I take your point, and in future I will ensure that my demo code does this, mainly because I don’t like code that doesn’t teach good practice. I think it’s a great idea to demonstrate Blendability (I really like that phrase), and I’ll incorporate it in future – that way, maybe I’ll be helping somebody else learn good things.

Leave a comment