用户控件中的数据上下文
UserControls 中的DataContext
通常是如何设置的?如果我在我的用户控件、窗口或其他控件中执行类似以下操作,
DataContext = this
当我想要将控件与绑定一起使用时,我将必须有一个 RelativeSource
来指向 Window/UserControl
<local:UserControl1 TextContent="{Binding Text1, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
Is在UserControls中绑定控件的方式:在UserControls中设置使用RelativeSource
而不是DataContext
?
<UserControl x:Class="SetCurrentValueTest.UserControl1" ...>
<TextBox Text="{Binding Path=TextContent, RelativeSource={RelativeSource AncestorType={x:Type local:UserControl1}}}" />
</UserControl>
How are DataContext
in UserControls usually set? If I do something like the below in my UserControl,
DataContext = this
In my Window or other Controls when I want to use the Control with a Binding, I will have to have a RelativeSource
to point to the Window/UserControl
<local:UserControl1 TextContent="{Binding Text1, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
Is the way to bind Controls within UserControls: set use RelativeSource
in UserControls instead of DataContext
?
<UserControl x:Class="SetCurrentValueTest.UserControl1" ...>
<TextBox Text="{Binding Path=TextContent, RelativeSource={RelativeSource AncestorType={x:Type local:UserControl1}}}" />
</UserControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否是您正在寻找的,但如果您想绑定到 UserControls 依赖属性,请使用此:
如果您想绑定到 UserControl 的 DataContext,请使用此绑定:
并不是我将 UserControl 指定为祖先- 输入而不是您的具体类型 (UserControl1)。
I dunno if it is what you are looking for, but if you want to bind to the UserControls Dependency-Properties, use this:
And if you want to bind to the UserControl's DataContext, use this Binding:
Not that I am specifying UserControl as Ancestor-Type and not your Concrete Type (UserControl1).