RoutedEventTrigger.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Microsoft.Xaml.Behaviors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. namespace PDF_Master.CustomControl.SystemControl
  9. {
  10. /// <summary>
  11. /// 用于辅助绑定ScrollChanged等不方便直接绑定command的系统
  12. /// </summary>
  13. public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
  14. {
  15. RoutedEvent _routedEvent;
  16. public RoutedEvent RoutedEvent
  17. {
  18. get { return _routedEvent; }
  19. set { _routedEvent = value; }
  20. }
  21. public RoutedEventTrigger()
  22. {
  23. }
  24. protected override void OnAttached()
  25. {
  26. Behavior behavior = base.AssociatedObject as Behavior;
  27. FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
  28. if (behavior != null)
  29. {
  30. associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
  31. }
  32. if (associatedElement == null)
  33. {
  34. throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
  35. }
  36. if (RoutedEvent != null)
  37. {
  38. associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
  39. }
  40. }
  41. void OnRoutedEvent(object sender, RoutedEventArgs args)
  42. {
  43. base.OnEvent(args);
  44. }
  45. protected override string GetEventName()
  46. {
  47. return RoutedEvent.Name;
  48. }
  49. }
  50. }