WP8 - 访问父级的数据上下文 [英] WP8 - access datacontext of parent

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

问题描述

如何在 windows phone 8 中访问父元素的数据上下文?AncestorType 在 WP8 中不可用.

How can I access the datacontext of the parent element in windows phone 8? AncestorType is not available in WP8.

<ItemsControl x:Name="Elements" ItemsSource="{Binding MyList}" Grid.Row="2" Grid.Column="3">
<ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel  />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="e"  Width="100" Height="100" Command="{Binding MyCommand" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

MyCommand"是在MyList"之外定义的.那么如何从我的按钮访问根数据上下文(DataContext = MyClass).MyCommand 在 MyClass 类中定义.

"MyCommand" is defined outside of "MyList". So how can I access from my button to the root datacontext (DataContext = MyClass). MyCommand is defined in the MyClass class.

提前致谢.

推荐答案

您可以改用 ElementName 绑定.如果您的根网格(直接位于页面内部的网格)称为 LayoutRoot:

You could use an ElementName Binding instead. If your root grid (the one directly inside your page) is called LayoutRoot:

<Button Command="{Binding DataContext.MyCommand, ElementName=LayoutRoot}" />

这篇关于WP8 - 访问父级的数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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