我在一个窗口中包含了一个列表(如下所示)。窗口的DataContext
有两个属性:Items
和AllowItemCommand
。
如何获取需要针对窗口的DataContext
解析的Hyperlink
的Command
属性的绑定
<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>
发布于 2009-07-14 20:51:39
您可以尝试如下所示:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
发布于 2016-04-24 17:16:49
这也是可行的:
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
Path=DataContext.AllowItemCommand}" />
ListView
将从Window
继承其DataContext
,因此现在也可以使用它。
由于ListView
就像类似的控件(如Gridview
、ListBox
等)一样,是ItemsControl
的子类,因此此类控件的Binding
将完美工作。
发布于 2012-04-11 14:43:29
这也适用于Silverlight5(可能之前也是如此,但我还没有测试过)。我像这样使用了相对源代码,它工作得很好。
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"
https://stackoverflow.com/questions/1127933
复制相似问题