Igor Meszaros
Igor Meszaros

Reputation: 2127

Hide empty context menu

I have a context menu in 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>

I amd changing the visibility for the two items in the view model, it is working fine. My problem is that when I set the visibility to false for both items, I have an empty white line when I open the context menu, and I don't know how to hide that... I tried:

<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>

But in this case I get a nullrefference exception...

How can I hide the context menu when it is empty?

Upvotes: 3

Views: 1316

Answers (1)

Igor Meszaros
Igor Meszaros

Reputation: 2127

I found this solution on a different forum.

        <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(); 
    } 

Setting e.Handled = true would block the ContextMenu from receiving the Hold event. In your view model you would implement some property or method that returns whether to display the ContextMenu.

Upvotes: 6

Related Questions