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?
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 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 meaningfull, 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.