WPF 数据绑定:如何访问“父"文件?数据上下文?
问题描述:
我有一个包含在窗口中的列表(见下文).窗口的 DataContext
有两个属性,Items
和 AllowItemCommand
.
I have a list (see below) contained in a window. The window's DataContext
has two properties, Items
and AllowItemCommand
.
如何获取Hyperlink
的Command
属性的绑定需要针对窗口的DataContext
进行解析?
How do I get the binding for the Hyperlink
's Command
property needs to resolve against the window's 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}" ...