Silverlight 访问控件的datacontext';s的父母';父母
我有一个像这样的xamlSilverlight 访问控件的datacontext';s的父母';父母,silverlight,windows-phone-7,xaml,Silverlight,Windows Phone 7,Xaml,我有一个像这样的xaml <Grid> <Grid.DataContext> <bin:ItemsCollViewModel/> </Grid.DataContext> <controls:Pivot ItemsSource="{Binding Items}"> <controls:Pivot.HeaderTemplate> <DataTemplate> &
<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>
....
我需要将上下文菜单项的command属性绑定到我的viewmodel(ItemsCallViewModel)中的ICommand属性(比如TryRemove)。此TryRemove属于ItemsCallViewModel。但其父级(透视)的DataContext是Items,这是ItemsCallViewModel中的一个属性。因此,如何将此上下文菜单项的command属性绑定到其父项(透视)父项(网格)的属性
简而言之,我如何访问控件父级的datacontext。
我知道了
但是编译器找不到AncestorType={x:Type Grid}
<ContextMenu>
<MenuItem Command="{Binding DatContext.TryRemove, RelativeSource={RelativeSource AncestorType={x:Type Grid},AncestorLevel=1} }"/>
</ContextMenu>
还可以尝试取消存储级别。我希望这会有所帮助。本着迟做总比不做好的精神: 将Name属性添加到Pivot控件并执行以下操作:
{Binding DataContext.TryRemove,ElementName=MyPivot}
感谢您的回答,但正如我在问题中所说,编译器无法找到AncestorType={x:Type Grid}。我想silverlight没有