2010-03-10 38 views
12

Tôi đang cố gắng tăng một MouseLeftButtonDownEvent bằng cách sủi bọt lên cây Visual với đoạn mã sau.Nâng cao WPF MouseLeftButtonDownEvent sự kiện

  MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice,0,  MouseButton.Left);    
     args.RoutedEvent = UIElement.MouseLeftButtonDownEvent; 
     args.Source = this; 
     RaiseEvent(args); 

Vì lý do nào đó, các thành phần cấp cao hơn không nhận được sự kiện sôi nổi này. Tôi có thấy điều gì đó hoặc không thể nâng cao sự kiện Chuột này

Trả lời

20

Vấn đề của bạn là bạn đang tăng sự kiện không phát sinh bong bóng.

MouseLeftButtonDownEvent được định nghĩa là RoutingStrategy.Direct, có nghĩa là nó được định tuyến tới chỉ điều khiển nhận sự kiện.

Bạn muốn sử dụng sự kiện Mouse.MouseDownEvent thay thế. UIElement và các lớp khác chuyển đổi nội bộ thành một số MouseLeftButtonDownEvent. Hãy chắc chắn rằng bạn thiết lập e.ChangedButton để MouseButton.Left:

RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) 
{ 
    RoutedEvent = Mouse.MouseDownEvent, 
    Source = this, 
}); 
0

Tôi có thể sai trong quan điểm của tôi - nhưng ít nhất tôi nhìn một số thời gian trước đây đối với một số chiều dài khá thành InputManager.

Sơ yếu lý lịch của tôi từ đó là: Việc tạo bọt và tạo đường hầm được thực hiện bởi InputManager. Tuy nhiên, gọi số uielement.Raise() sẽ chỉ bao giờ cung cấp sự kiện trực tiếp (bất kể số RoutingStrategy như Ray Burns đã đề cập đến).

Nhưng (đoán) tùy thuộc vào RoutingStrategy các InputManager đi lên và xuống cây thị giác giữa CompositionRootVisualTreeHlper.Hittest()- ed thị giác và cung cấp đường hầm và các sự kiện bublling.

Có một cách để nâng cao sự kiện thông qua InputManager, nhưng nó không phải là chính thức và cần phản ánh (Tôi có nó từ một bài Stackoverflow):

void RaiseMouseInputReportEvent(Visual eventSource, int timestamp, int pointX, int pointY, int wheel) 
    { 
     Assembly targetAssembly = Assembly.GetAssembly(typeof(InputEventArgs)); 
     Type mouseInputReportType = targetAssembly.GetType("System.Windows.Input.RawMouseInputReport"); 

     Object mouseInputReport = mouseInputReportType.GetConstructors()[0].Invoke(new Object[] { 
     InputMode.Foreground, timestamp, PresentationSource.FromVisual(eventSource), 
     RawMouseActions.AbsoluteMove | RawMouseActions.Activate, 
     pointX, pointY, wheel, IntPtr.Zero }); 

     mouseInputReportType.GetField("_isSynchronize", BindingFlags.NonPublic | BindingFlags.Instance) 
      .SetValue(mouseInputReport, true); 

     InputEventArgs inputReportEventArgs = (InputEventArgs)targetAssembly 
      .GetType("System.Windows.Input.InputReportEventArgs") 
      .GetConstructors()[0] 
      .Invoke(new Object[] { 
      Mouse.PrimaryDevice, 
      mouseInputReport }); 

     inputReportEventArgs.RoutedEvent = (RoutedEvent)typeof(InputManager) 
      .GetField("PreviewInputReportEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) 
      .GetValue(null); 

     bool handled = InputManager.Current.ProcessInput((InputEventArgs)inputReportEventArgs); 
    } 
Các vấn đề liên quan