WPF 如何绑定两个嵌套用户控件

发布于 2024-10-08 08:54:30 字数 444 浏览 0 评论 0原文

我需要将用户控件数据上下文与其他控件属性绑定:

    <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-10-15 08:54:30

您如何定义 MySelectedValue DependencyProperty (是否设置为 BindsTwoWayByDefault )?

How did you define the MySelectedValue DependencyProperty (is it set to BindsTwoWayByDefault)?

一个人的夜不怕黑 2024-10-15 08:54:30

弗拉德(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文