从 UserControl 引发 MouseButtonEvent 时,MainWindow 无法访问 MouseButtonEventArgs

发布于 2024-10-17 11:52:24 字数 1094 浏览 6 评论 0原文

还在爬陡峭的WPF山,很痛苦。

我已经定义了一个 UserControl,我的 MainWindow 需要检索来自 UserControl 内的控件的 MouseButtonEventArgs(例如鼠标 e.GetPosition)。

在后面的 UserControl 代码中,我已经完成了注册并引发了冒泡事件。

public static readonly RoutedEvent MyButtonDownEvent = EventManager.RegisterRoutedEvent("MyMouseButtonDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));
    public event RoutedEventHandler MyButtonDown {
        add { AddHandler(MyButtonDownEvent, value); }
        remove { RemoveHandler(MyButtonDownEvent, value); }
    }
    private void MyMouseButtonDownHandler(object sender, MouseButtonEventArgs e) {
        RaiseEvent(new RoutedEventArgs(MyButtonDownEvent ));
    }

现在在我的 MainWindow 中,我像这样声明 UserControl:

<local:MyUserControl MouseDown="MyUserControl_MouseDown"/>

后面的代码

private void MyUserControl_MouseDown(object sender, RoutedEventArgs e) 

我从 UserControl 接收事件,但参数是 RoutedEventArgs (这是正常的),但我无权访问我需要获取鼠标 e 的 MouseButtonEventArgs .获取位置。

在这种情况下,您会建议什么优雅的解决方案?

Still climbing up the steep WPF Mountain, and suffering.

I have defined a UserControl, and my MainWindow needs to retrieve the MouseButtonEventArgs coming from a control inside the UserControl (like the mouse e.GetPosition for instance)

In the UserControl code behind, I have done the Registrations and I Raise the bubbling event.

public static readonly RoutedEvent MyButtonDownEvent = EventManager.RegisterRoutedEvent("MyMouseButtonDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));
    public event RoutedEventHandler MyButtonDown {
        add { AddHandler(MyButtonDownEvent, value); }
        remove { RemoveHandler(MyButtonDownEvent, value); }
    }
    private void MyMouseButtonDownHandler(object sender, MouseButtonEventArgs e) {
        RaiseEvent(new RoutedEventArgs(MyButtonDownEvent ));
    }

Now in my MainWindow I declare the UserControl like this:

<local:MyUserControl MouseDown="MyUserControl_MouseDown"/>

And this code behind

private void MyUserControl_MouseDown(object sender, RoutedEventArgs e) 

And I receive the events from the UserControl, but the Args are RoutedEventArgs (which is normal) but I dont have access to the MouseButtonEventArgs that I need to get the mouse e.GetPosition.

What elegant solution would you suggest in this case ?

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

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

发布评论

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

评论(2

孤星 2024-10-24 11:52:24

为什么您要定义自己的 MouseDown 事件,而 UserControl 已经有一个普通的 MouseDown 事件?

无论如何,如果您定义一个事件来使用 RoatedEventHandler,那么您最终会陷入 RoatedEventHandler 的困境也就不足为奇了。您是这样声明的:

public static readonly RoutedEvent MyButtonDownEvent = EventManager.RegisterRoutedEvent("MyMouseButtonDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));

请注意其中的 typeof(RoatedEventHandler) 部分吗?

如果我没有记错的话,您的代码应该如下所示:

    public static readonly RoutedEvent MyButtonDownEvent =
        EventManager.RegisterRoutedEvent
        ("MyButtonDown",
        RoutingStrategy.Bubble,
        typeof(MouseButtonEventHandler),
        typeof(MyUserControl));

    public event MouseButtonEventHandler MyButtonDown
    {
        add { AddHandler(MyButtonDownEvent, value); }
        remove { RemoveHandler(MyButtonDownEvent, value); }
    }

如何将现有 MouseDown 事件传播到自定义事件的示例:

InitializeComponent();
this.MouseDown += (s, e) => {
    RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton)
                    { 
                        RoutedEvent = MyButtonDownEvent
                    });
};

Why do you define your own MouseDown event while UserControl already has a normal MouseDown event?

Anyway, if you define an event to use a RoutedEventHandler it is hardly surprising that you'll end up being stuck with a RoutedEventHandler. You declared it like this:

public static readonly RoutedEvent MyButtonDownEvent = EventManager.RegisterRoutedEvent("MyMouseButtonDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));

Notice the bit where it says typeof(RoutedEventHandler)?

If i am not mistaken your code should look like this instead:

    public static readonly RoutedEvent MyButtonDownEvent =
        EventManager.RegisterRoutedEvent
        ("MyButtonDown",
        RoutingStrategy.Bubble,
        typeof(MouseButtonEventHandler),
        typeof(MyUserControl));

    public event MouseButtonEventHandler MyButtonDown
    {
        add { AddHandler(MyButtonDownEvent, value); }
        remove { RemoveHandler(MyButtonDownEvent, value); }
    }

Example of how to propagate an existing MouseDown event to the custom event:

InitializeComponent();
this.MouseDown += (s, e) => {
    RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton)
                    { 
                        RoutedEvent = MyButtonDownEvent
                    });
};
无人接听 2024-10-24 11:52:24

我想我终于明白了(至少我希望):

如果我在后面的代码中编写:

        public event EventHandler<MouseButtonEventArgs> MyRightButtonDownHandler;
    public void MyRightButtonDown(object sender, MouseButtonEventArgs e) {
        MyRightButtonDownHandler(sender, e);
    }

然后在消费者(MainWindow)XAML中:

<local:GlobalDb x:Name="globalDb"  MyRightButtonDownHandler="globalDb_MyRightButtonDownHandler"/>

在消费者代码后面:

    private void globalDb_MyRightButtonDownHandler(object sender, MouseButtonEventArgs e) {
        Console.WriteLine("x= " + e.GetPosition(null).X + " y= " + e.GetPosition(null).Y);
    }

请告诉我你是否有更好的解决方案(根据设计政策) - 在我工作的地方建立的规则 - 我的应用程序的所有事件处理必须出现在 XAML 中)。

再次感谢您的帮助,

I think I finally got it (at least I hope):

If I write in the code behind:

        public event EventHandler<MouseButtonEventArgs> MyRightButtonDownHandler;
    public void MyRightButtonDown(object sender, MouseButtonEventArgs e) {
        MyRightButtonDownHandler(sender, e);
    }

Then in the consumer (MainWindow) XAML:

<local:GlobalDb x:Name="globalDb"  MyRightButtonDownHandler="globalDb_MyRightButtonDownHandler"/>

And in the consumer code behind:

    private void globalDb_MyRightButtonDownHandler(object sender, MouseButtonEventArgs e) {
        Console.WriteLine("x= " + e.GetPosition(null).X + " y= " + e.GetPosition(null).Y);
    }

Please tell me if you have a better solution (By design policy - rules established where I work - all the event handling of my application MUST appear in the XAML).

Thanks again for your help,

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