WPF数据绑定:我如何访问“父”数据上下文?

我有一个窗口中包含一个列表(见下文)。 窗口的DataContext有两个属性, ItemsAllowItemCommand

如何获取HyperlinkCommand属性需要根据窗口的DataContextparsing的DataContext

 <ListView ItemsSource="{Binding Items}"> <ListView.View> <GridView> <GridViewColumn Header="Action"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <TextBlock> <!-- this binding is not working --> <Hyperlink Command="{Binding AllowItemCommand}" CommandParameter="{Binding .}"> <TextBlock Text="Allow" /> </Hyperlink> </TextBlock> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> 

你可以尝试这样的事情:

 ...Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ... 

这也将工作:

 <Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.AllowItemCommand}" /> 

ListView将从Windowinheritance它的DataContext ,所以在这一点上也是可用的。
ListView ,就像类似的控件(例如GridviewListBox等)是ItemsControl的子类,所以这些控件的Binding将完美工作。

这也适用于Silverlight 5(也许更早,但我还没有testing过)。 我用这样的相对来源,它工作得很好。

RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"