开发者

Bind Control to DependancyProperty of container control

开发者 https://www.devze.com 2023-03-10 08:15 出处:网络
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

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 ...

  1. 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 your UserControl.
  2. Use a ReltiveSource FindAncestor binding to navigate up the visual tree from your ListBox to your UserControl.
  3. Use an ElementName binding, by giving your UserControl a name and referencing that in your ListBox binding.

Personally I favour (1), typically there will be more that one property you want to expose, making this the simplest solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消