Contextmenu - disable right click to open it.

I have xaml code like this:

<Grid x:Name="boardGrid">
    <Grid.ContextMenu>
        <ContextMenu Opacity="0.7" x:Name="menuContext">

        </ContextMenu>
    </Grid.ContextMenu>
</Grid>

      

I am generating grid items in the code behind. I want to disable the context menu by right clicking. I want to open it when certification conditions arise.

This is what I have in the .cs file:

  • generates Unit objects and places them in the Grid;

each object has unit.MouseRightButtonUp + = unit_MouseRightButton

void unit_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    if (CurrentGame.CurrentPlayer.HasTurn == false) return; 
    .....
    ContextMenu.IsOpen = true;

}

      

So, this means that the Contextmenu should only be opened if the condition is met, but it still opens.

+3


source to share


1 answer


You can set the attached property ContextMenuService.IsEnabled

to false

. Then you can manually open the context menu.

You must set this property on the GUI element that the menu belongs to. Setting it to the menu itself will do nothing.



<Grid x:Name="boardGrid" ContextMenuService.IsEnabled="false">
    <!-- ... -->
</Grid>

      

void unit_MouseRightButtonUp(object sender, MouseButtonEventArgs e) {
    if (CurrentGame.CurrentPlayer.HasTurn == false) return; 
    .....
    boardGrid.ContextMenu.IsOpen = true;
}

      

+10


source







All Articles