仍然爬上陡峭的WPF山,並且感到痛苦。當從UserControl引發MouseButtonEvent時,MainWindow無法訪問MouseButtonEventArgs
我已經定義了一個用戶控件,我的主窗口需要檢索MouseButtonEventArgs從用戶控件裏的控件來(例如像鼠標e.GetPosition)
在後面的用戶控件的代碼,我也做了註冊記憶我舉起冒泡事件。
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));
}
現在在我的主窗口,我宣佈類似這樣的用戶控件:
<local:MyUserControl MouseDown="MyUserControl_MouseDown"/>
背後
private void MyUserControl_MouseDown(object sender, RoutedEventArgs e)
這個代碼,我收到來自用戶控件的事件,但ARG遊戲RoutedEventArgs (這是正常的),但我沒有訪問MouseButtonEventArgs,我需要得到鼠標e.GetPosition。
在這種情況下,你會建議什麼優雅的解決方案?
找到了解決方案 –
是的,這就是我的意思,它比使用路由事件更簡單,當然有時您的事件需要路由 –