WP8 - 访问父级的datacontext

use*_*039 6 xaml binding commandbinding windows-phone-8

如何在windows phone 8中访问父元素的datacontext?WP8中没有AncestorType.

<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>
Run Code Online (Sandbox Code Playgroud)

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

提前致谢.

小智 13

您可以使用ElementName绑定.如果你的根网格(直接在你的页面内)被称为LayoutRoot:

<Button Command="{Binding DataContext.MyCommand, ElementName=LayoutRoot}" />
Run Code Online (Sandbox Code Playgroud)