WPF 如何绑定两个嵌套用户控件
我需要将用户控件数据上下文与其他控件属性绑定:
<StackPanel>
<custom:UserControl1 x:Name="UC1"/>
</StackPanel>
<StackPanel>
<custom:UserControl2 DataContext="{Binding ElementName=UC1, Path=MySelectedValue}"/>
</StackPanel>
当然,我已经在 UserControl1 上实现了依赖属性“MySelectedValue”。 以下不起作用。我猜是因为它在可视化树上搜索 ElementName 但找不到它,因为两个控件都是并行嵌套的。
我怎样才能让上述工作?
谢谢,
I need to bind a user control data context with other control property :
<StackPanel>
<custom:UserControl1 x:Name="UC1"/>
</StackPanel>
<StackPanel>
<custom:UserControl2 DataContext="{Binding ElementName=UC1, Path=MySelectedValue}"/>
</StackPanel>
Of course i have implemented a dependency property 'MySelectedValue' on UserControl1.
The following does not work. I guess because it searches the ElementName on the visual tree and could not find it because both controls are parallel nested.
How could i get the above to work?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何定义 MySelectedValue DependencyProperty (是否设置为 BindsTwoWayByDefault )?
How did you define the MySelectedValue DependencyProperty (is it set to BindsTwoWayByDefault)?
弗拉德(Vlad)为我解决了这个问题,并提到一切实际上都很好!
我的依赖属性没有正确定义。
即使元素与调用元素并行嵌套,“ElementName”绑定似乎仍然有效。
谢谢。
Vlad had solved this for me mentioning that everything is actually fine!
I had my dependency property not defined properly.
It seems that the 'ElementName' binding works even if the element is nested on parallel with the calling element.
Thank you.