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

时间:2015-04-01 12:55:32

标签: c# wpf canvas contextmenu right-click

我有一个画布,当用户右键单击它时,会出现一个上下文菜单。我也有一个复选框,当选中该框时,我不希望出现上下文菜单。这样做的原因是,当选中该复选框时,用户的前两次右键单击将在右键单击的两个点处删除省略号。但是现在,右键单击右键会弹出上下文菜单。这是相关代码:

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

以及右键单击画布背后的代码:

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

我已经尝试过使用上下文菜单的isOpen属性,但无论是否设置为true或false,它都会在右键单击时打开。

尝试下面的转换器。如果这是正确的,使用它来绑定复选框和上下文菜单的正确方法是什么?

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 个答案:

答案 0 :(得分:1)

我会实现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>

答案 1 :(得分:0)

这是我弄清楚如何做到这一点的方式,因为我无法让转换器正常工作:

在画布中将ContextMenuService.IsEnabled属性设置为false。然后,在后面的代码中,在未选中scalebox时设置nodeContextMenu.IsOpen = true。这似乎可以解决问题。

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

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