Silverlight:如何防止将 MouseMove 事件从子画布路由到其父画布

发布于 2024-12-27 07:54:58 字数 1285 浏览 2 评论 0原文

我有我的 XAML 代码:

<Canvas x:Name="mainCanvas" Width="200" Height="150" Background="LightGray"
        MouseLeftButtonUp="mainCanvas_MouseLeftButtonUp"
        MouseMove="mainCanvas_MouseMove">
    <Canvas x:Name="topCanvas" Width="200" Height="100" Background="LightBlue"
            MouseLeftButtonUp="topCanvas_MouseLeftButtonUp"
            MouseMove="topCanvas_MouseMove">
    </Canvas>
</Canvas>

及其背后的代码:

private void topCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("topCanvas_MouseLeftButtonUp");

    e.Handled = true; // This can prevent routing to the mainCanvas
}

private void mainCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("mainCanvas_MouseLeftButtonUp");
}

private void topCanvas_MouseMove(object sender, MouseEventArgs e)
{
    Debug.WriteLine("topCanvas_MouseMove");

    // How to prevent routing to the mainCanvas?
    // e.Handled = true does NOT exist in MouseEventArgs
}

private void mainCanvas_MouseMove(object sender, MouseEventArgs e)
{
    Debug.WriteLine("mainCanvas_MouseMove");
}

我的问题已经在评论中。

如何防止将 MouseMove 事件从 topCanvas(子画布)路由到 mainCanvas(父画布)?

谢谢。

彼得

I have my XAML code:

<Canvas x:Name="mainCanvas" Width="200" Height="150" Background="LightGray"
        MouseLeftButtonUp="mainCanvas_MouseLeftButtonUp"
        MouseMove="mainCanvas_MouseMove">
    <Canvas x:Name="topCanvas" Width="200" Height="100" Background="LightBlue"
            MouseLeftButtonUp="topCanvas_MouseLeftButtonUp"
            MouseMove="topCanvas_MouseMove">
    </Canvas>
</Canvas>

and its code behind:

private void topCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("topCanvas_MouseLeftButtonUp");

    e.Handled = true; // This can prevent routing to the mainCanvas
}

private void mainCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("mainCanvas_MouseLeftButtonUp");
}

private void topCanvas_MouseMove(object sender, MouseEventArgs e)
{
    Debug.WriteLine("topCanvas_MouseMove");

    // How to prevent routing to the mainCanvas?
    // e.Handled = true does NOT exist in MouseEventArgs
}

private void mainCanvas_MouseMove(object sender, MouseEventArgs e)
{
    Debug.WriteLine("mainCanvas_MouseMove");
}

My question is already in the comments.

How to prevent routing the MouseMove event from the topCanvas (the child canvas) to the mainCanvas (parent canvas)?

Thanks.

Peter

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

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

发布评论

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

评论(3

时光瘦了 2025-01-03 07:54:58

尝试设置 Canvas 的 IsHitTestVisible 属性。相应地设置该属性后,鼠标事件将“通过”您的控件或被它捕获。

希望这是您所需要的。

Try setting the IsHitTestVisible property of your Canvas. With that property set accordingly mouse events will go either "through" your control or will be caught by it.

Hope this is what you need.

月野兔 2025-01-03 07:54:58

您可以尝试比较 mainCanvas 的 MouseMove 事件中的 e.OriginalSource,如果它不是源自 mainCanvas,则退出 Sub。

private void mainCanvas_MouseMove(object sender, MouseEventArgs e)
{
    if (sender != e.OriginalSource)
        return;        
}

更详细地回复您的评论。根据 UIElement。 MouseMove 事件 MSDN 链接。

继承MouseMove的控件可以提供对事件的处理
通过重写 OnMouseMove 充当所有实例的处理程序
方法。与直接处理事件一样,没有 Handled
属性可用,因此OnMouseMove不能以这种方式实现
它通过 Handled 抑制对事件的进一步处理
技术。

链接指出:

此事件为 Mouse.MouseMove 附加事件创建别名
这个类

将我们带到 AttachedEvents 上的链接,其中指出。

可扩展应用程序标记语言 (XAML) 定义了一种语言
称为附加事件的事件的组件和类型。的概念
附加事件使您能够为特定事件添加处理程序
到任意元素而不是实际的元素
定义或继承事件。在这种情况下,对象既不
可能引发事件或目标处理实例
定义或以其他方式“拥有”该事件。

所以在我看来,你唯一的选择就是围绕它编写代码。

You can try comparing e.OriginalSource in mainCanvas's MouseMove Event and exit the Sub if it wasn't originated from the mainCanvas.

private void mainCanvas_MouseMove(object sender, MouseEventArgs e)
{
    if (sender != e.OriginalSource)
        return;        
}

In replying to your comment in a little more detail. According to the UIElement.MouseMove Event MSDN link.

Controls that inherit MouseMove can provide handling for the event
that acts as handler for all instances, by overriding the OnMouseMove
method. As with direct handling of the event, there is no Handled
property available, so OnMouseMove cannot be implemented in such a way
that it suppresses further handling of the event through the Handled
technique.

and this link states:

This event creates an alias for the Mouse.MouseMove attached event for
this class

Which brings us to this link on AttachedEvents which states.

Extensible Application Markup Language (XAML) defines a language
component and type of event called an attached event. The concept of
an attached event enables you to add a handler for a particular event
to an arbitrary element rather than to an element that actually
defines or inherits the event. In this case, neither the object
potentially raising the event nor the destination handling instance
defines or otherwise "owns" the event.

So as I see it, your only option is to code around it.

爱*していゐ 2025-01-03 07:54:58

该功能称为“事件冒泡”。您可以使用以下代码停止它:

jQuery:
event.stopPropagation();
参考: http://api.jquery.com/event.stopPropagation/

您也可以尝试下面的代码:
e.stopPropagation(); //防止事件冒泡
e.preventDefault(); //然后取消该事件(如果可以取消)

Ref: http://stackoverflow.com/questions/1967537/how-to-stop-event-bubbling-with-jquery-live

谢谢,
拉维·维尔马

The functionality is called "Event Bubbling". You can stop it using below code:

jQuery:
event.stopPropagation();
Ref: http://api.jquery.com/event.stopPropagation/

You can also try below code:
e.stopPropagation(); //to prevent event from bubbling up
e.preventDefault(); //then cancel the event (if it's cancelable)

Ref: http://stackoverflow.com/questions/1967537/how-to-stop-event-bubbling-with-jquery-live

Thanks,
Ravi Verma

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文