WPF数据绑定:如何访问“父”数据上下文? [英] WPF Databinding: How do I access the "parent" data context?

查看:300
本文介绍了WPF数据绑定:如何访问“父”数据上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口中包含的列表(见下文)。窗口的 DataContext 有两个属性,项目 AllowItemCommand

I have a list (see below) contained in a window. The window's DataContext has two properties, Items and AllowItemCommand.

如何获取超链接的绑定命令 property需要解决窗口的 DataContext

How do I get the binding for the Hyperlink's Command property needs to resolve against the window's 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}" ...

这篇关于WPF数据绑定:如何访问“父”数据上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆