慕哥9229398
2019-08-06 14:52:18
WPF數(shù)據(jù)綁定:如何訪問(wèn)“父”數(shù)據(jù)上下文?我有一個(gè)窗口中包含的列表(見(jiàn)下文)。窗口DataContext有兩個(gè)屬性,Items和AllowItemCommand。如何獲取針對(duì)窗口的屬性需要解析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陽(yáng)
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
你可以嘗試這樣的事情:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...

慕村225694
TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
這也有效:
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.AllowItemCommand}" />
ListView
將繼承其DataContext
從Window
,所以它的使用在這一點(diǎn)上,太。
并且,因?yàn)?code>ListView,就像類似的控件(例如Gridview
,ListBox
等等)是其子類ItemsControl
,Binding
對(duì)于這樣的控件將完美地工作。

臨摹微笑
TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
這也適用于Silverlight 5(也許更早,但我還沒(méi)有測(cè)試過(guò))。我使用了這樣的相對(duì)來(lái)源,它工作得很好。
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"
添加回答
舉報(bào)
0/150
提交
取消