我在wp7中有一个上下文菜单
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="onHoldContextMenu">
<toolkit:MenuItem Header="Delete" Tag="{Binding}" Click="DeleteVisitorNote_Click" Visibility="{Binding DeleteContextVisibility, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<toolkit:MenuItem Header="View" Tag="{Binding}" Visibility="{Binding ViewContextVisibility, Converter={StaticResource BooleanToVisibilityConverter}}" Click="ViewVisitorContact_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
我正在改变视图模型中两个项目的可见性,它工作正常。我的问题是,当我将两个项目的可见性设置为false时,当我打开上下文菜单时,我有一个空的白线,我不知道如何隐藏...我试过了:
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="onHoldContextMenu" Visibility="{Binding ContextVisibility, Converter={StaticResource BooleanToVisibilityConverter}}">
<toolkit:MenuItem Header="Delete" Tag="{Binding}" Click="DeleteVisitorNote_Click" Visibility="{Binding DeleteContextVisibility, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<toolkit:MenuItem Header="View" Tag="{Binding}" Visibility="{Binding ViewContextVisibility, Converter={StaticResource BooleanToVisibilityConverter}}" Click="ViewVisitorContact_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
但是在这种情况下我得到了一个nullrefference异常......
如何在空白时隐藏上下文菜单?
答案 0 :(得分:6)
我在另一个论坛上找到了这个解决方案。
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Hold="Grid_Hold">
<toolkit:ContextMenuService.ContextMenu>
... context menu items ...
</toolkit:ContextMenuService.ContextMenu>
... listbox item contents ...
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
private void Grid_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
ItemViewModel listitem = (sender as FrameworkElement).DataContext as ItemViewModel;
e.Handled = listitem.DisableContextMenu();
}
设置e.Handled = true
会阻止ContextMenu
收到Hold
事件。在视图模型中,您将实现一些返回是否显示ContextMenu
。