2012-07-16 46 views
1

我有这样访问的DataContext

<Grid> 
<Grid.DataContext> 
    <bin:ItemsCollViewModel/> 
</Grid.DataContext> 
<controls:Pivot ItemsSource="{Binding Items}"> 
    <controls:Pivot.HeaderTemplate> 
     <DataTemplate> 
      <Grid> 
       <TextBlock Text="{Binding ItemName}" />        
       <toolkit:ContextMenuService.ContextMenu> 
        <toolkit:ContextMenu > 
         <toolkit:ContextMenu.Items>           
          <toolkit:MenuItem Header="Remove" /> 
         </toolkit:ContextMenu.Items> 
        </toolkit:ContextMenu> 
       </toolkit:ContextMenuService.ContextMenu> 
      </Grid> 
     </DataTemplate> 
    </controls:Pivot.HeaderTemplate> 
    <controls:Pivot.ItemTemplate> 
     .... 
    </controls:Pivot.ItemTemplate> 
</controls:Pivot> 

我需要在我的视图模型上下文菜单项到一个ICommand财产(说TryRemove)命令属性绑定的XAML (ItemsCollViewModel)。此TryRemove属于ItemsCollViewModel。但它的父的DataContext的(透视)是物品这在ItemsCollViewModel的性质。那么如何将此上下文菜单项的命令属性绑定到其父(数据透视表)父(网格)的属性。

简而言之,我如何访问控件的Parent's Parent的datacontext。 我得到这个How to change the DataContext of a button to the parents parent DataContext? 但编译器无法找到AncestorType = {X:式格栅}

回答

0
<ContextMenu> 
       <MenuItem Command="{Binding DatContext.TryRemove, RelativeSource={RelativeSource AncestorType={x:Type Grid},AncestorLevel=1} }"/> 
      </ContextMenu> 

尝试AncestorLevel也。我希望这将有助于。

+1

谢谢回答,但我在我的问题编译器说找不到* * AncestorType = {x:Type Grid} **。我认为它不适用于silverlight – 2012-07-17 04:22:15

0

在比亡羊补牢永远的精神:

Name属性添加到您的枢轴控件并执行以下操作: {Binding DataContext.TryRemove, ElementName=MyPivot}