首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WPF数据绑定:如何访问“父”数据上下文?

WPF数据绑定:如何访问“父”数据上下文?
EN

Stack Overflow用户
提问于 2009-07-14 20:46:44
回答 3查看 193.7K关注 0票数 231

我在一个窗口中包含了一个列表(如下所示)。窗口的DataContext有两个属性:ItemsAllowItemCommand

如何获取需要针对窗口的DataContext解析的HyperlinkCommand属性的绑定

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-07-14 20:51:39

您可以尝试如下所示:

代码语言:javascript
复制
...Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
票数 447
EN

Stack Overflow用户

发布于 2016-04-24 17:16:49

这也是可行的:

代码语言:javascript
复制
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
                             Path=DataContext.AllowItemCommand}" />

ListView将从Window继承其DataContext,因此现在也可以使用它。

由于ListView就像类似的控件(如GridviewListBox等)一样,是ItemsControl的子类,因此此类控件的Binding将完美工作。

票数 49
EN

Stack Overflow用户

发布于 2012-04-11 14:43:29

这也适用于Silverlight5(可能之前也是如此,但我还没有测试过)。我像这样使用了相对源代码,它工作得很好。

RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1127933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档