Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP8 - access datacontext of parent

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.

like image 462
user1288039 Avatar asked Apr 16 '13 12:04

user1288039


1 Answers

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}" />
like image 196
Jeremy Gilbert Avatar answered Oct 16 '22 19:10

Jeremy Gilbert