C# 无法检测右键单击鼠标下键和上键事件

C# 无法检测右键单击鼠标下键和上键事件,c#,wpf,C#,Wpf,我已经在Windows7中编写了WPF软件VS2019。 我想实现这个功能:当我右键单击鼠标并在画布中绘制一条线时,将绘制一条线,从我右键单击鼠标时开始,到我释放右键单击时结束 我尝试了很多方法,但程序似乎无法检测右键单击事件 <UserControl x:Class="STIGenericReport.UserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati

我已经在Windows7中编写了WPF软件VS2019。 我想实现这个功能:当我右键单击鼠标并在画布中绘制一条线时,将绘制一条线,从我右键单击鼠标时开始,到我释放右键单击时结束

我尝试了很多方法,但程序似乎无法检测右键单击事件

<UserControl x:Class="STIGenericReport.UserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="407" Width="633" Background="White" BorderBrush="White" Focusable="False">
    <Grid Name="maingrid" Background="White" Height="405" Width="616" MinWidth="500">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="619*" />
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height ="574*" />
            <RowDefinition Height="26" MinHeight="16" />
        </Grid.RowDefinitions>
    <Viewport3D Name="mainViewport" ClipToBounds = "False"  
                IsHitTestVisible="false"
                RenderOptions.EdgeMode="Aliased" Margin="12,52,144,47">
        <Viewport3D.Camera>
            <OrthographicCamera x:Name="camera" Width="2"
                FarPlaneDistance="10"
                NearPlaneDistance="1" 
                LookDirection="0,0,-1"
                UpDirection="0,1,0"
                Position="0,0,2" />
        </Viewport3D.Camera>
        <Viewport3D.Children>
            <ModelVisual3D x:Name="Light1">
                <ModelVisual3D.Content>
                    <DirectionalLight Color="White" Direction="1, 1, -1" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="Light2">
                <ModelVisual3D.Content>
                    <DirectionalLight Color="White" Direction="-1, 1, -1" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="Light3">
                <ModelVisual3D.Content>
                    <DirectionalLight Color="White" Direction="0,-1,-0.5" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D.Children>
     </Viewport3D>

    <Canvas x:Name="canvasOn3D" Background="#00E6FFFF" 
            Margin="20,52,102,18"></Canvas>
    <Label Content="Trayinfo    &amp;" FontSize="16" FontWeight="Bold" Height="34" HorizontalAlignment="Left" Margin="192,0,0,0" Name="lblTrayinfo" VerticalAlignment="Top" Width="194" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,257" Name="btn9" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,232" Name="btn8" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button HorizontalAlignment="Left" Margin="499,149,0,207" Name="btn7" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,182" Name="btn6" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,157" Name="btn5" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,132" Name="btn4" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,107" Name="btn3" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,82" Name="btn2" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,57" Name="btn1" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Button Height="25" HorizontalAlignment="Left" Margin="499,0,0,282" Name="btn10" VerticalAlignment="Bottom" Width="50" Background="White" BorderThickness="0" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,57" Name="lbl1" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,82" Name="lbl2" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,107" Name="lbl3" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,132" Name="lbl4" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,157" Name="lbl5" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,182" Name="lbl6" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,207" Name="lbl7" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,232" Name="lbl8" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,257" Name="lbl9" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Height="25" HorizontalAlignment="Right" Margin="0,0,12,282" Name="lbl10" VerticalAlignment="Bottom" Width="45" Background="White" BorderBrush="White" />
    <Label Content="Pacakage info" HorizontalAlignment="Center" Width="Auto" Height="32" Name="lblPkgname" FontSize="16" FontWeight="Bold" Margin="300,0,191,347" />
</Grid>

当我调试这个程序时,当我右键单击鼠标时,它不会进入这个功能。

Previewmouserightbuttondown即使在一个简单的实验中也似乎不起作用

您的代码中也有一些奇怪之处

鼠标的坐标应该基于画布(大概是这样)

下面是一些更简单的工作标记和代码

在Mainwindow中,我刚刚在默认网格中添加了一个画布:

    <Grid>
        <Canvas 
            Name="lineCanvas"
            Background="Transparent"
            PreviewMouseDown="lineCanvas_PreviewMouseDown"
            PreviewMouseUp="lineCanvas_PreviewMouseUp"
                >
        
        </Canvas>
    </Grid>

请注意,在我们的地图编辑器中,我对绘制直线进行了类似的处理,我在鼠标轮后面添加了一条“橡皮筋”临时线,显示该线的终点。这会再次向用户保证正在发生某些事情。你可能想考虑这个方法。

但是左按钮工作?否则,您可能会忘记设置画布的背景属性。它不会在空背景下获取鼠标输入。是的,左键工作。在我的例子中,空背景是什么意思?但是您已经附加了处理程序方法?在您的XAML中没有类似的内容。请通过提供所有相关细节来帮助我们。您已经使用边距布置了所有控件,可能是通过在设计器表面上拖动它们来实现的。这是一种非常糟糕的工作方式。您可以很容易地让控件彼此重叠。您应该使用网格中的列和行来排列内容。使用stackpanels来承载一组控件需要一个接一个地排列。PreviewMouseRightButtonDown/up对我来说很好。没有明显的理由不应该这样做。奇怪。可能取决于驱动程序。您有多个画布?这和你的问题有什么关系?
    <Grid>
        <Canvas 
            Name="lineCanvas"
            Background="Transparent"
            PreviewMouseDown="lineCanvas_PreviewMouseDown"
            PreviewMouseUp="lineCanvas_PreviewMouseUp"
                >
        
        </Canvas>
    </Grid>
    public MainWindow()
    {
        InitializeComponent();
    }
    Point startPoint = new Point();

    private void lineCanvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Right)
        {
            startPoint = Mouse.GetPosition(lineCanvas);
        }
    }

    private void lineCanvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (startPoint.X == 0 && startPoint.Y == 0)
        {
            return;
        }
        var currentPoint = Mouse.GetPosition(lineCanvas);
        Line line = new Line();
        line.Stroke = SystemColors.WindowFrameBrush;
        line.X1 = startPoint.X;
        line.Y1 = startPoint.Y;
        line.X2 = currentPoint.X;
        line.Y2 = currentPoint.Y;
        line.StrokeThickness = 2;
        lineCanvas.Children.Add(line);

        startPoint = new Point();
    }