2013-04-16 5 views
6

Windows Phone 8の親要素のdatacontextにはどのようにアクセスできますか? AncestorTypeはWP8では使用できません。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(DataContext = MyClass)にアクセスするにはどうしたらいいですか? MyCommandは、MyClassクラスで定義されています。

ありがとうございます。

答えて

13

代わりにElementNameバインディングを使用できます。ルートグリッド(ページのすぐ内側にあるもの)がLayoutRootという名前の場合:

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

ありがとう – user1288039

関連する問題