WPF 与 PlacementTarget 和relativeSource 绑定
您能解释一下以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来像是用于弹出元素的 hack,例如 ContextMenus 和 Popup-windows。
这些元素的问题是,它们与窗口的可视化树断开连接。因此,
DataContext
不可用。PlacementTarget
是指向视觉树元素的链接。大多数情况下,您会发现类似
PlacementTarget.Tag
的绑定路径,其中在源元素中Tag
属性已设置为DataContext
但在某些情况下,元素本身也是有意义的,例如在您的示例中。假设上面的代码用在
ToolTip
或ContextMenu
中,DataContext
将被设置为“拥有”该元素的控件。请参阅 (Gishu +1) 的帖子了解机制的解释。
This looks like a hack that is used for popup-elements such as
ContextMenus
andPopup
-windows.The problem with these elements is, that they are disconnected from the visual tree of your window. Therefore the
DataContext
is not available. ThePlacementTarget
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 theTag
property has been set to theDataContext
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 aContextMenu
, theDataContext
will be set to the control that "owns" the element.Look at the post from (Gishu +1) for an explanation of the mechanics.
每个
FrameworkElement
都有一个DataContext
,它是任意对象。数据绑定的默认源是DataContext
。您可以使用RelativeSource.Self
将绑定源更改为FrameworkElement
本身,而不是其DataContext
。因此,RelativeSource
部分只是将您从FrameworkElement
的DataContext
移动到FrameworkElement
本身。到达FrameworkElement
后,您可以指定其任何属性的路径。如果FrameworkElement
是一个Popup
,它将有一个PlacementTarget
属性,该属性是FrameworkElement
>Popup 相对于定位。简而言之,例如,如果您有一个相对于
TextBox
放置的Popup
,则该表达式会设置Popup
的DataContext
code> 到TextBox
,结果Popup
正文中的{Binding Text}
将绑定到的文本>文本框
。Every
FrameworkElement
has aDataContext
that is an arbitrary object. The default source for a data binding is thatDataContext
. You can useRelativeSource.Self
to change the source for a binding to theFrameworkElement
itself instead of itsDataContext
. So theRelativeSource
part just moves you "up one level" from theDataContext
of theFrameworkElement
to theFrameworkElement
itself. Once you are at theFrameworkElement
you can specify a path to any of its properties. If theFrameworkElement
is aPopup
, it will have aPlacementTarget
property that is the otherFrameworkElement
that thePopup
is positioned relative to.In short, if you have a
Popup
placed relative to aTextBox
for example, that expression sets theDataContext
of thePopup
to theTextBox
and as a result{Binding Text}
somewhere in the body of thePopup
would bind to the text of theTextBox
.这是将某个事物(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.