Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Silverlight UserControl to a complex object

What is the "best" (or the most-often-used) approach when binding a complex class to a user control for re-use?

I am trying to create some reusable libraries for classes, and I am not sure which approach I should use. Example: I want to create an Address library that defines and Address class (with properties Line1, Line2 etc.), it's validation logic and an AddressControl which acts as the viewer/editor with bound fields for each property.

In use I might have a customer class with BillingAddress, DeliveryAddress properties and I would want to bind these in my customer control thus:

<addressLib:AddressControl [xxx]="{Binding BillingAddress}" />

So the question is what do I put in XXX?

Initially I thought of creating a DependencyProperty 'Address' on the control:

<addressLib:AddressControl Address="{Binding BillingAddress}" />

But now I am thinking surely I could just use the existing DataContext property?

<addressLib:AddressControl DataContext="{Binding BillingAddress}" />

Is this the best approach? Are there any issues e.g. updates or NotifyPropertyChange issues?

many thanks for your help!

like image 427
Quango Avatar asked Jul 27 '26 21:07

Quango


2 Answers

One difference to remember is that with dependency property you get change notification and with datacontext you don't (at least not untill SL5 is out)

like image 57
Denis Avatar answered Jul 30 '26 09:07

Denis


Setting the DataContext of your control to your model (in this case a BillingAddress) is the way to go. If your Control is used in a DataTemplate for an ItemsControl then the DataContext would be of course just "{Binding}".

like image 23
Oliver Weichhold Avatar answered Jul 30 '26 10:07

Oliver Weichhold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!