ScrollViewer (Preview)MouseButtonDown 事件顺序中的画布 [英] Canvas in ScrollViewer (Preview)MouseButtonDown event order

查看:45
本文介绍了ScrollViewer (Preview)MouseButtonDown 事件顺序中的画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有

<ScrollViewer Name="scroll_viewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Canvas Name="canvas" Height="200" Width="200">
        <Rectangle Fill="AliceBlue" Width="100" Height="100"/>  
    </Canvas>
</ScrollViewer> 

带有处理程序:

scroll_viewer.PreviewMouseLeftButtonDown
scroll_viewer.MouseLeftButtonDown
canvas.PreviewMouseLeftButtonDown

然后,如果我们在矩形中单击,我们会先调用 scroll_viewer_PreviewMouseLeftButtonDown,然后调用 canvas_PreviewMouseLeftButtonDown,但不会调用 scroll_viewer_MouseLeftButtonDown.
我想首先在画布中处理单击事件 - 如果单击了一个对象,我想处理该事件(用于对象拖动).如果没有点击画布对象,我想在 scroll_viewer 中处理事件(用鼠标管理滚动视图平移).
鉴于调用顺序与我想要的相反并且未调用非查看版本 scroll_viewer.MouseLeftButtonDown,如何管理此问题?

Then if we click in the Rectangle we get scroll_viewer_PreviewMouseLeftButtonDown called first then canvas_PreviewMouseLeftButtonDown but scroll_viewer_MouseLeftButtonDown is not called.
I want to handle the click event first in the canvas - if an object is clicked I want to handled the event (for object drag). If no canvas object is clicked I want to handle event in scroll_viewer (to manage scrollview panning with the mouse).
How to manage this given that the call order is the oposite of what i want and that the non perview version scroll_viewer.MouseLeftButtonDown is not called?

更新:
来自这篇文章:Silverlight 论坛

((FrameworkElement)scroll_viewer.GetValue(ScrollViewer.ContentProperty)).MouseLeftButtonDown += scroll_viewer_MouseLeftButtonDown;

DOES work 即在预览事件后被调用 - 有人可以解释为什么需要这种不太明显的语法吗?

DOES work ie does get called after the preview events - can some explain why this less than obvious syntax is required?

推荐答案

问题是 ScrollViewer 已经在内部处理了 MouseLeftButtonDown 事件,如下所示:

The problem is that the ScrollViewer already handles the MouseLeftButtonDown event internally, like so:

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
    if (base.Focus())
        e.Handled = true;
    base.OnMouseLeftButtonDown(e);
}

您可以使用自定义类修复"此问题,如下所示:

You can "fix" this using a custom class, like so:

public class MyScrollViewer : ScrollViewer {

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
        base.OnMouseLeftButtonDown(e);
        e.Handled = false;
    }
}

边注:您应该在 XAML 中使用 x:Name,而不是 Name.否则,您可能会在使用上述类时遇到编译错误.

SIDE NOTE: You should use x:Name in XAML, not Name. Otherwise you may run into compilation errors using the above class.

或者,您可以为所有 MouseLeftButtonDown 事件附加处理程序,包括已处理的事件.所以,而不是:

Alternatively, you could attach your handler for all MouseLeftButtonDown events, including handled ones. So instead of:

this.scroll_viewer.MouseLeftButtonDown += new MouseButtonEventHandler(scroll_viewer_MouseLeftButtonDown);

你会使用:

this.scroll_viewer.AddHandler(ScrollViewer.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.scroll_viewer_MouseLeftButtonDown), true);

这篇关于ScrollViewer (Preview)MouseButtonDown 事件顺序中的画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆