简体   繁体   English

在画布上禁止右键单击上下文菜单

[英]Suppressing right-click context menu on canvas

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. 我尝试弄乱上下文菜单的isOpen属性,但是无论它设置为true还是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();
    }
}
}

I would implement a ValueConverter or MultiValueConverter and then bind to the checkbox using the converter to dictate the state (ie enabled/disabled) of the contextmenu. 我将实现一个ValueConverterMultiValueConverter ,然后使用转换器绑定到复选框,以指示上下文菜单的状态(即启用/禁用)。

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

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. 在画布中将ContextMenuService.IsEnabled属性设置为false。 Then, in the code behind, set nodeContextMenu.IsOpen = true when the scalebox is not checked. 然后,在后面的代码中,当未选中scalebox时,设置nodeContextMenu.IsOpen = true。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM