简体   繁体   English

WPF,某些事件未从网格触发到用户控件

[英]WPF, Some Event not fire up from Grid To UserControl

I have a UserControl where i override some event like this: 我有一个UserControl,我在其中重写了这样的事件:

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. 我将此控件添加到另一个UserControl-> Grid,此Grid已注册MouseUp。

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

And in MyParent XAML i have simply: 在MyParent XAML中,我只需:

<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? 我注意到的是,当我将鼠标移到MyControl上时,该事件被Grid捕获而不被路由到MyControl,为什么?

How can i receive MouseUp event inside MyControl class? 我如何在MyControl类中接收MouseUp事件?

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? 编辑 MouseDown可以正常工作,只有MouseUp不能工作...而且两个事件也都为父Grid注册,所以有什么区别?

EDIT2 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. 好的,我认为我发现了问题,如果我直接在XAML中将MyControl添加到MyParent-> Grid中,则一切正常,但是如果我以编程方式将其添加到“ MyParentInstance.Grid.Children.Add(MyControlInstance)”,那么我会遇到上述问题。

Is my code to add control correct? 我添加控件的代码正确吗?

Thanks. 谢谢。

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. 将ChildControl用户控件添加到顶层的Window时,这些控件将触发。 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: 以编程方式添加控件时,请在Parent控件中添加事件处理程序,例如:

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
    }
}

RoutedEvent s only work for the parents of the element, which is invoking the specific event. RoutedEvent仅适用于元素的父级,后者正在调用特定事件。 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: 为了解决您的问题,我建议在MyControl上注册MouseUp事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM