为什么 MouseLeftButtonUp 在 WPF 中不触发?

发布于 2024-11-24 12:07:44 字数 1801 浏览 1 评论 0原文

为什么 Canvas 上的 MouseLeftButtonUp 在 WPF 应用程序中没有触发? 这是 XAML:

<Grid Height="300" Width="400">
    <Canvas Name="canvas" MouseMove="canvas_MouseMove" MouseLeftButtonUp="canvas_MouseLeftButtonUp" Background="LightGray"/>
</Grid>

和代码:

    private bool hasClicked = false;

    public Window1()
    {
        InitializeComponent();
    }

    private void canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (!this.hasClicked)
        {
            this.Cursor = Cursors.None;
            this.canvas.Children.Clear();
            this.insertRectangle(false);
        }
    }

    private void insertRectangle(bool filled)
    {
        Rectangle rect = createRect(filled);
        Point pos = Mouse.GetPosition(this.canvas);
        Canvas.SetLeft(rect, pos.X);
        Canvas.SetTop(rect, pos.Y);
        this.canvas.Children.Add(rect);
    }

    private Rectangle createRect(bool fill)
    {
        Rectangle rect = new Rectangle();
        rect.Height = 50;
        rect.Width = 120;
        if (fill)
        {
            rect.Fill = new SolidColorBrush(Colors.Green);
        }
        else
        {
            rect.Stroke = new SolidColorBrush(Colors.Green);
        }
        return rect;
    }

    private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.hasClicked = true;
        this.insertRectangle(true);
        this.Cursor = Cursors.Arrow;
    }

编辑:我尝试向画布添加背景颜色,但事件仍然没有触发。看起来 MouseMove 以某种方式覆盖了 MouseLeftButtonUp。

Edit2:如果我删除 MouseMove 事件,mouseLeftButtonUp 将触发。

Edit3:更大的代码示例。 在 insertRectangle 方法中,如果我使用

 Canvas.SetTop(rect, 50);
 instead of 
 Canvas.SetTop(rect, pos.Y);

事件触发就好了。

Why does not the MouseLeftButtonUp on my Canvas fire in my WPF app?
Here is the XAML:

<Grid Height="300" Width="400">
    <Canvas Name="canvas" MouseMove="canvas_MouseMove" MouseLeftButtonUp="canvas_MouseLeftButtonUp" Background="LightGray"/>
</Grid>

And the code:

    private bool hasClicked = false;

    public Window1()
    {
        InitializeComponent();
    }

    private void canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (!this.hasClicked)
        {
            this.Cursor = Cursors.None;
            this.canvas.Children.Clear();
            this.insertRectangle(false);
        }
    }

    private void insertRectangle(bool filled)
    {
        Rectangle rect = createRect(filled);
        Point pos = Mouse.GetPosition(this.canvas);
        Canvas.SetLeft(rect, pos.X);
        Canvas.SetTop(rect, pos.Y);
        this.canvas.Children.Add(rect);
    }

    private Rectangle createRect(bool fill)
    {
        Rectangle rect = new Rectangle();
        rect.Height = 50;
        rect.Width = 120;
        if (fill)
        {
            rect.Fill = new SolidColorBrush(Colors.Green);
        }
        else
        {
            rect.Stroke = new SolidColorBrush(Colors.Green);
        }
        return rect;
    }

    private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.hasClicked = true;
        this.insertRectangle(true);
        this.Cursor = Cursors.Arrow;
    }

Edit: I have tried adding a background colour to the canvas, but still the event is not fired. It seems like the MouseMove somehow overrides the MouseLeftButtonUp.

Edit2: If I remove the MouseMove event, mouseLeftButtonUp will fire.

Edit3: Bigger code example.
In the insertRectangle method, if I use

 Canvas.SetTop(rect, 50);
 instead of 
 Canvas.SetTop(rect, pos.Y);

the events fires just fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云不回头 2024-12-01 12:07:44

如果您没有在画布上设置Background,它似乎不会关注您的鼠标事件。

尝试:

<Grid>
    <Canvas Name="canvas"
            MouseMove="canvas_MouseMove"
            MouseLeftButtonUp="canvas_MouseLeftButtonUp"
            Background="White" />
</Grid>

If you don't set a Background on the canvas, it doesn't seem to pay attention to your mouse events.

Try:

<Grid>
    <Canvas Name="canvas"
            MouseMove="canvas_MouseMove"
            MouseLeftButtonUp="canvas_MouseLeftButtonUp"
            Background="White" />
</Grid>
枫以 2024-12-01 12:07:44

通过不清除画布,而是在 mouseMove 方法中移动预览矩形解决了问题。

By not clearing the canvas, but instead moving the preview rectangle in the mouseMove method solved the problem.

铁轨上的流浪者 2024-12-01 12:07:44

我的解决方案是完全放弃 MouseLeftButtonUp 处理程序,而是对 MouseMove 进行初始检查以查看按钮是否处于释放状态。如果是的话我将拖动设置为 false。

private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    isDragging = true;
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Released)
    {
        isDragging = false;
    }
}

My solution was to abandon the MouseLeftButtonUp handler altogether and instead do an initial check on MouseMove to see if the button was in a released state. If it was then I set dragging to false.

private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    isDragging = true;
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Released)
    {
        isDragging = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文