Ответ 1
Вы можете попробовать что-то вроде этого:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
У меня есть список (см. ниже), содержащийся в окне. Окно DataContext
имеет два свойства: Items
и AllowItemCommand
.
Как мне получить привязку для свойства Hyperlink
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>
Вы можете попробовать что-то вроде этого:
...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
и т.д.), Является подклассом ItemsControl
, Binding
для таких элементов управления будет работать отлично.
Это также работает в Silverlight 5 (возможно, раньше, но я его не тестировал). Я использовал относительный источник, как это, и он работал нормально.
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"