views:

693

answers:

1

I have a small usercontrol that basically increments or decrements a value by one. The user control has two buttons(one to add and the other to subtract) and a textBlock that is used to display the value.

I am going to have multiple instance of this usercontrol in another usercontrol so I can manipulate values of a dataclass that has an INotifyPropertyChanged interface. My question is how can I databind the textBlock of the value changing usercontrol to the usercontrol I instansiated it in?

+3  A: 

First, I want to state that Silverlight 2 does not support element to element binding. That feature is added in Silverlight 3 (out in Beta now). Having said that, I don't think you want to bind controls together anyway. It sounds like you're trying to build a NumericUpDown control and you probably have some class in code behind that's actually doing the incrementing and decrementing.

If that's the case, you can simply subscribe to the click handlers and call a method on your model like Increment or Decrement. Your model can expose a property for the current value and that property is what is bound to your text box.

Now if you're actually trying to build a NumericUpDown control, you might want to check out the Silverlight Toolkit. The toolkit already includes this control and it also supports data binding.

Check out the NumericUpDown Control here and download the toolkit here.

Finally, binding from a child control to a parent control really isn't any different. The parent UserControl has a DataContext and all child controls inherit that. Each individual child control can also have its DataContext set. Binding expressions are always relative to the DataContext and the DataContext can be set in code. In your case, probably to a model of some sort.

I hope that helps.

Jared Bienz - MSFT