0
votes

I have a canvas and when a user right clicks on it, a context menu appears. I also have a checkbox, and when that box is checked, I DON'T want the context menu to appear. The reason for this is that, when the checkbox is checked, the first two right clicks from the user will drop ellipses at the two points of the right clicks. Right now though, the context menu will popup on those two right clicks. Here's the relative code:

<Window x:Class="Testproj.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Testproj"
    xmlns:localConverters="clr-namespace:Testproj"
    x:Name="this"
    Height="650" Width="1091"
    Loaded="this_Loaded"
    Closing="this_Closing">

    <Window.Resources>
        <local:BoolToVisibilityConverter x:Key="converter"/>
    </Window.Resources>

<Grid Height="Auto">
    <Grid.Resources>
        <local:NullToVisibilityConverter x:Key="nullToVisibilityConverter" />
    </Grid.Resources>

    <Grid VerticalAlignment="Top">
        <DockPanel>
                <CheckBox x:Name="scaleBox" Content="Scale" IsChecked="False" Checked="scaleischecked"/>
            </Menu>
        </DockPanel>
    </Grid>

    <Viewbox Margin="0,23,0,157" x:Name="viewbox1" ClipToBounds="True">
        <Canvas Margin="0,21,0,12" x:Name="canvas1" ClipToBounds="True" RenderOptions.BitmapScalingMode="HighQuality" MouseWheel="Canvas_Zoom" MouseRightButtonDown="get_MousePosition"  HorizontalAlignment="Left" Width="3138" Height="1260">
            <Canvas.RenderTransform>
                <MatrixTransform x:Name="mt"/>
            </Canvas.RenderTransform>
            <Canvas.ContextMenu>
                <ContextMenu Name="nodeContextMenu" Visibility="{StaticResource converter}" >
                    <MenuItem x:Name="test1"  IsCheckable="False" Header="test1" Click="WaypointMenuItem_Click" >
                    </MenuItem>
                    <MenuItem x:Name="test2" IsCheckable="False" Header="test2" Click="KnownObjectMenuItem_Click" >
                    </MenuItem>
                </ContextMenu>
            </Canvas.ContextMenu>
        </Canvas>
    </Viewbox>
</Grid>
</Window>

and the code behind for the right-click on canvas:

    private void get_MousePosition(object sender, MouseButtonEventArgs e)
    {
        if (scaleBox.IsChecked == true)
        {
            get_points(sender, e);
        }
    }

I've tried messing around with the isOpen property of the context-menu, but it always open on right click regardless if it's set to true or false.

Attempt at converter below. If this is correct, what is the proper way to bind the checkbox and contextmenu using this?

namespace Testproj
{
public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Visibility visibility = Visibility.Collapsed;
        if (value != null)
        {
            visibility = (bool)value ? Visibility.Collapsed : Visibility.Visible;
        }

        return visibility;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
}
2

2 Answers

1
votes

I would implement a ValueConverter or MultiValueConverter and then bind to the checkbox using the converter to dictate the state (i.e. enabled/disabled) of the contextmenu.

        <Canvas.ContextMenu>
            <ContextMenu Name="contextmenu1" Visibility="{Binding ElementName=scaleBox, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" >
                <MenuItem x:Name="item1"  IsCheckable="False" Header="item2" />
                <MenuItem x:Name="item2" IsCheckable="False" Header="item1" />
            </ContextMenu>
0
votes

Here's the way I figured out how to do it since I couldn't get the converter to work properly:

Set the ContextMenuService.IsEnabled property to false in the canvas. Then, in the code behind, set nodeContextMenu.IsOpen = true when the scalebox is not checked. This seems to do the trick.

<Canvas Margin="0,21,0,12" x:Name="canvas1" ContextMenuService.IsEnabled="False" />

if (scaleBox.IsChecked == true)
{
    get_Scaling(sender, e);
}
else
{
    nodeContextMenu.IsOpen = true;
}