WP8 - access datacontext of parent

此生再无相见时 提交于 2020-01-21 07:13:25

问题


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" 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.

Thanks in advance.


回答1:


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}" />


来源:https://stackoverflow.com/questions/16037096/wp8-access-datacontext-of-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!