Jor*_*dan 202 data-binding wpf datacontext
我有一个窗口中包含的列表(见下文).窗口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>
Run Code Online (Sandbox Code Playgroud)
flq*_*flq 398
你可以尝试这样的事情:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
Run Code Online (Sandbox Code Playgroud)
Kyl*_*Ren 28
这也有效:
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
Path=DataContext.AllowItemCommand}" />
Run Code Online (Sandbox Code Playgroud)
ListView将继承其DataContext从Window,所以它的使用在这一点上,太.
因为ListView,就像类似的控件(例如Gridview,ListBox等等)是一个子类ItemsControl,Binding对于这样的控件将完美地工作.
小智 7
这也适用于Silverlight 5(也许更早,但我还没有测试过).我使用了这样的相对来源,它工作得很好.
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"
| 归档时间: |
|
| 查看次数: |
160280 次 |
| 最近记录: |