WPF數據綁定:如何訪問“父”數據上下文?我有一個窗口中包含的列表(見下文)。窗口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>
3 回答
BIG陽
TA貢獻1859條經驗 獲得超6個贊
你可以嘗試這樣的事情:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
慕村225694
TA貢獻1880條經驗 獲得超4個贊
這也有效:
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
Path=DataContext.AllowItemCommand}" />ListView將繼承其DataContext從Window,所以它的使用在這一點上,太。
并且,因為ListView,就像類似的控件(例如Gridview,ListBox等等)是其子類ItemsControl,Binding對于這樣的控件將完美地工作。
臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
這也適用于Silverlight 5(也許更早,但我還沒有測試過)。我使用了這樣的相對來源,它工作得很好。
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"
添加回答
舉報
0/150
提交
取消
