wpf 相对来源和弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14939632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RelativeSource and Popup
提问by Alexander Boriskin
The problem is that RelativeSourcedoes not work in the following case. I use silverlight 5.
问题是RelativeSource在以下情况下不起作用。我用的是银光5。
//From MainPage.xaml
<Grid x:Name="LayoutRoot" Background="White" Height="100" Width="200">
<Popup IsOpen="True">
<TextBlock Text="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=Grid}}" />
</Popup>
</Grid>
//From MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
DataContext = "ololo";
}
If I set a breakpoint on the binding, I'll get Error:
如果我在绑定上设置断点,我会得到错误:
System.Exception: BindingExpression_CannotFindAncestor.
System.Exception: BindingExpression_CannotFindAncestor。
If I use ElementName=LayoutRootinstead of RelativeSource, everything will be OK.
如果我使用ElementName=LayoutRoot而不是RelativeSource,一切都会好的。
Why does the relative source binding not work?
为什么相对源绑定不起作用?
采纳答案by yo chauhan
Popup is like ContextMenu , ToolTip controls , They are not added to the VisualTree. For this you will have to do like
Popup 就像 ContextMenu 、 ToolTip 控件,它们没有添加到 VisualTree 中。为此,您必须这样做
<Grid x:Name="LayoutRoot" Height="100" Width="200" Background="Black">
<Popup Grid.Row="0" x:Name="popup" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Mode=Self}}">
<TextBlock Text="{Binding DataContext, ElementName=popup}" Background="Red" Width="30" Height="30" />
</Popup>
</Grid>
public MainWindow()
{
InitializeComponent();
DataContext = "abcd";
popup.PlacementTarget = LayoutRoot;
}
I hope this will help.Not like in case of ContextMenu or Tooltip , here you will also have to specify the PlacementTarget.
我希望这会有所帮助。不像 ContextMenu 或 Tooltip 的情况,在这里您还必须指定 PlacementTarget。
回答by 17 of 26
As others have mentioned, it's because the Popup is not part of the visual tree. Instead, you can use the Popup's PlacementTargetproperty to get back to the visual tree:
正如其他人所提到的,这是因为 Popup 不是可视化树的一部分。相反,您可以使用 Popup 的PlacementTarget属性返回可视化树:
<Grid x:Name="LayoutRoot" Background="White" Height="100" Width="200">
<Popup IsOpen="True">
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Popup}},
Path=PlacementTarget.DataContext}" />
</Popup>
</Grid>
回答by Rover
You can make small hack: setup DataContext via resources.
您可以进行一些小技巧:通过资源设置 DataContext。
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="DataContext" Value="{Binding ElementName=myGrid, Path=DataContext}" />
</Style>
</Grid.Resources>
回答by user2055119
Popups are not part of the visual tree.
弹出窗口不是可视化树的一部分。
Relative Source "Gets or sets the binding source by specifying its location relative to the position of the binding target (MSDN)". Since Popups are not part of the visual tree of the control that is showing it, it will not be able to resolve anything outside of the popup.
相对源“通过指定其相对于绑定目标 (MSDN) 的位置的位置来获取或设置绑定源”。由于弹出窗口不是显示它的控件的可视化树的一部分,因此它将无法解析弹出窗口之外的任何内容。

