WP8 - parent datacontext access

How can I access the datacontext of the parent in a Windows 8 phone? 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.

+6
source share
1 answer

Instead, you can use the ElementName binding. If your root grid (one directly inside your page) is called LayoutRoot:

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

All Articles