Привязка данных WPF: как получить доступ к "родительскому" контексту данных?
у меня есть список (см. ниже), содержащиеся в окно. Окно DataContext
есть два свойства, Items
и AllowItemCommand
.
как получить привязку для Hyperlink
' s Command
свойство необходимо разрешить против окна 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>
3 ответов
вы можете попробовать что-то вроде этого:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
это также будет работать:
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
Path=DataContext.AllowItemCommand}" />
ListView
наследование DataContext
С Window
, поэтому он доступен и на данный момент.
И с ListView
так же, как аналогичные элементы управления (Эл. г. Gridview
, ListBox
, etc.), является подклассом ItemsControl
на Binding
для таких элементов управления будет работать отлично.
Это также работает в Silverlight 5 (возможно и раньше, но я не тестировал). Я использовал относительный источник, и он работал нормально.
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"