WPF,某些事件未从网格到用户控件启动

发布于 2025-01-01 04:10:23 字数 1221 浏览 0 评论 0原文

我有一个 UserControl,我在其中重写一些事件,如下所示:

public class MyControl: UserControl
{
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonUp(e);
        // handle mouse event up
    }
}

我将此控件添加到另一个 UserControl -> Grid,这个Grid已经注册了MouseUp。

public class MyParent: UserControl
{
    private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
    {
            // handle grid mouse event
    }
}

在 MyParent XAML 中,我很简单:

<UserControl ... >
        <Grid Name="Grid" MouseUp="Grid_MouseUp">
               <my:MyControl Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" />
        </Grid>
</UserControl>

我注意到,当我在 MyControl 上释放鼠标时,事件被 Grid 捕获,而不是路由到 MyControl,为什么?

如何在 MyControl 类中接收 MouseUp 事件?

编辑 MouseDown 全部按预期工作,只有 MouseUp 不起作用...并且这两个事件也为父 Grid 注册,那么有什么区别呢?

EDIT2

好吧,我想我发现了问题,如果我将 MyControl 添加到 MyParent ->直接在 XAML 中网格,一切正常,但如果我以编程方式添加它“MyParentInstance.Grid.Children.Add(MyControlInstance)”,那么我就会遇到上述问题。

我添加控件的代码正确吗?

谢谢。

I have a UserControl where i override some event like this:

public class MyControl: UserControl
{
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonUp(e);
        // handle mouse event up
    }
}

I add this control to antother UserControl -> Grid, this Grid has MouseUp registered.

public class MyParent: UserControl
{
    private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
    {
            // handle grid mouse event
    }
}

And in MyParent XAML i have simply:

<UserControl ... >
        <Grid Name="Grid" MouseUp="Grid_MouseUp">
               <my:MyControl Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" />
        </Grid>
</UserControl>

What i notice is that when i release mouse over MyControl the event is captured by Grid and not routed to MyControl, why?

How can i receive MouseUp event inside MyControl class?

EDIT
With MouseDown all works as expected, only MouseUp not works... and both event are registered for parent Grid too, so what is the difference?

EDIT2

Ok, i think i found the problem, if i add MyControl to MyParent -> Grid directly in XAML, all works good, but if i add it programmatically "MyParentInstance.Grid.Children.Add(MyControlInstance)" then i have the problem above.

Is my code to add control correct?

Thanks.

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

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

发布评论

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

评论(2

如果没有你 2025-01-08 04:10:23

RoutedEvent 仅适用于调用特定事件的元素的父元素。这对您来说意味着网格的所有父控件都将收到该事件,但子控件不会。有关路由事件的详细信息,请参阅路由事件概述。为了解决您的问题,我建议在 MyControl 中注册 MouseUp 事件:

<UserControl ... >
  <Grid Name="Grid">
    <my:MyControl MouseUp="Grid_MouseUp" Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" />
  </Grid>
</UserControl>

RoutedEvents only work for the parents of the element, which is invoking the specific event. That means for you, that all parent controls of your grid will receive the event, but children will not. For more information about routed events see Routed Events Overview. To solve your problem I would suggest to register the MouseUp Event at MyControl:

<UserControl ... >
  <Grid Name="Grid">
    <my:MyControl MouseUp="Grid_MouseUp" Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" />
  </Grid>
</UserControl>
ぃ双果 2025-01-08 04:10:23

经过广泛搜索论坛后,我没有找到这个问题的答案,所以我在这里提供我的解决方案,尽管这篇文章已经过时了。我通过将事件处理程序公开来利用了我已经在子控件中构建的事件处理程序。当 ChildControl 用户控件添加到顶层的 Window 时,这些控件将被触发。当嵌套在父级中时,必须在父级处理该事件。

以编程方式添加控件时,请在父控件中添加事件处理程序,例如:

class Parent : UIElement
{
    //...

    void Parent_AddChildControl()
    {
        ChildControl child = new ChildControl();
        child.MouseEnter += child_MouseEnter;
        UiElementX.Children.Add(child);
    }

    void child_MouseEnter( object sender , MouseEventArgs e )
    {
        ((ChildControl)sender).ChildControl_MouseEnter(sender, e);
    }
}

class ChildControl : UIElement
{
    //...

    public void ChildControl_MouseEnter(object sender, MouseEventArgs e)
    {
        //event handling
    }
}

I did not come across the answer to this question after an extensive search of forums so I'm providing my solution here although this post is dated. I made use of event handlers that I'd already built in the child control by making them public. When the ChildControl user control is added to the Window at the top level, these fire. When nested within parent, the event had to be handled at the parent level.

When programmatically adding a control, add the event handler in the Parent control, such as:

class Parent : UIElement
{
    //...

    void Parent_AddChildControl()
    {
        ChildControl child = new ChildControl();
        child.MouseEnter += child_MouseEnter;
        UiElementX.Children.Add(child);
    }

    void child_MouseEnter( object sender , MouseEventArgs e )
    {
        ((ChildControl)sender).ChildControl_MouseEnter(sender, e);
    }
}

class ChildControl : UIElement
{
    //...

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