WPF 与 PlacementTarget 和relativeSource 绑定

发布于 2024-10-17 14:58:39 字数 181 浏览 1 评论 0原文

您能解释一下以下 WPF 代码吗:

DataContext="{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}">

我发现它非常令人困惑。什么是放置目标,什么是相对来源?

Can you explain the following WPF code:

DataContext="{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}">

I find it extremely confusing. What is placement target and what is relative source?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

2024-10-24 14:58:39

这看起来像是用于弹出元素的 hack,例如 ContextMenus 和 Popup-windows。
这些元素的问题是,它们与窗口的可视化树断开连接。因此,DataContext 不可用。 PlacementTarget 是指向视觉树元素的链接。
大多数情况下,您会发现类似 PlacementTarget.Tag 的绑定路径,其中在源元素中 Tag 属性已设置为 DataContext 但在某些情况下,元素本身也是有意义的,例如在您的示例中。

假设上面的代码用在 ToolTipContextMenu 中,DataContext 将被设置为“拥有”该元素的控件。

请参阅 (Gishu +1) 的帖子了解机制的解释。

This looks like a hack that is used for popup-elements such as ContextMenus and Popup-windows.
The problem with these elements is, that they are disconnected from the visual tree of your window. Therefore the DataContext is not available. The PlacementTarget is a link to an element of the visual-tree.
Mostly you will find a binding path like PlacementTarget.Tag where in the source element the Tag property has been set to the DataContext but in some situations, the element itself is also meaningful, such as in your example.

Assuming that the above code is used in a ToolTip or a ContextMenu, the DataContext will be set to the control that "owns" the element.

Look at the post from (Gishu +1) for an explanation of the mechanics.

卸妝后依然美 2024-10-24 14:58:39

每个FrameworkElement都有一个DataContext,它是任意对象。数据绑定的默认源是DataContext。您可以使用 RelativeSource.Self 将绑定源更改为 FrameworkElement 本身,而不是其 DataContext。因此,RelativeSource 部分只是将您从 FrameworkElementDataContext 移动到 FrameworkElement 本身。到达 FrameworkElement 后,您可以指定其任何属性的路径。如果 FrameworkElement 是一个 Popup,它将有一个 PlacementTarget 属性,该属性是 FrameworkElement >Popup 相对于定位。

简而言之,例如,如果您有一个相对于 TextBox 放置的 Popup,则该表达式会设置 PopupDataContext code> 到 TextBox,结果 Popup 正文中的{Binding Text} 将绑定到 的文本>文本框

Every FrameworkElement has a DataContext that is an arbitrary object. The default source for a data binding is that DataContext. You can use RelativeSource.Self to change the source for a binding to the FrameworkElement itself instead of its DataContext. So the RelativeSource part just moves you "up one level" from the DataContext of the FrameworkElement to the FrameworkElement itself. Once you are at the FrameworkElement you can specify a path to any of its properties. If the FrameworkElement is a Popup, it will have a PlacementTarget property that is the other FrameworkElement that the Popup is positioned relative to.

In short, if you have a Popup placed relative to a TextBox for example, that expression sets the DataContext of the Popup to the TextBox and as a result {Binding Text} somewhere in the body of the Popup would bind to the text of the TextBox.

岁月无声 2024-10-24 14:58:39

这是将某个事物(UI 控件?需要查看更多代码片段)的 DataContext 绑定到它自己的 PlacementTarget 属性值。

RelativeSource 用于指示相对于绑定目标的源对象。路径属性指示源对象上的属性名称。

This is binding the DataContext of a thing (UI Control? need to see more of the code snippet ) to its own PlacementTarget property value.

RelativeSource is used to indicate the source object relative to the binding target. The path property indicates the name of the property on the source object.

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