Bind Control to DependancyProperty of container control
I have created a UserControl in Silverlight and added some dependency properties to it to expose selected opt开发者_运维百科ions in the control to any other controls that use my control, how do I bind properties of something like a ListBox used in my control to the dependency properties of my control? I am currently using ElementBinding but I'm not too fond of ElementBinding in general... I was hoping there is some sort of relative binding but relative binding doesn't seem to provide the options I am looking for (but I don't think I actually understand relative binding very well)
So you have a UserControl
tha contains a ListBox
, and you wish to expose your ListBox
selected items from your UserControl
?
I can think of a few options ...
- Set the DataContext of the 'LayoutRoot' of your UserControl to itself, as in this example, you can then bind the
ListBox.SelectedItem
to your corresponding property on yourUserControl
. - Use a ReltiveSource FindAncestor binding to navigate up the visual tree from your ListBox to your
UserControl
. - Use an
ElementName
binding, by giving yourUserControl
a name and referencing that in yourListBox
binding.
Personally I favour (1), typically there will be more that one property you want to expose, making this the simplest solution.
精彩评论