从 UserControl 引发 MouseButtonEvent 时,MainWindow 无法访问 MouseButtonEventArgs
还在爬陡峭的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么您要定义自己的
MouseDown
事件,而UserControl
已经有一个普通的 MouseDown 事件?无论如何,如果您定义一个事件来使用
RoatedEventHandler
,那么您最终会陷入RoatedEventHandler
的困境也就不足为奇了。您是这样声明的:请注意其中的
typeof(RoatedEventHandler)
部分吗?如果我没有记错的话,您的代码应该如下所示:
如何将现有 MouseDown 事件传播到自定义事件的示例:
Why do you define your own
MouseDown
event whileUserControl
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 aRoutedEventHandler
. You declared it like this:Notice the bit where it says
typeof(RoutedEventHandler)
?If i am not mistaken your code should look like this instead:
Example of how to propagate an existing MouseDown event to the custom event:
我想我终于明白了(至少我希望):
如果我在后面的代码中编写:
然后在消费者(MainWindow)XAML中:
在消费者代码后面:
请告诉我你是否有更好的解决方案(根据设计政策) - 在我工作的地方建立的规则 - 我的应用程序的所有事件处理必须出现在 XAML 中)。
再次感谢您的帮助,
I think I finally got it (at least I hope):
If I write in the code behind:
Then in the consumer (MainWindow) XAML:
And in the consumer code behind:
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,