6 using System.Windows.Data;
7 using System.Windows.Interactivity;
9 using SiliconStudio.Presentation.Core;
11 namespace SiliconStudio.Presentation.Behaviors
22 public static readonly DependencyProperty EventNameProperty = DependencyProperty.Register(
"EventName", typeof(
string), typeof(
OnEventBehavior));
27 public static readonly DependencyProperty EventOwnerTypeProperty = DependencyProperty.Register(
"EventOwnerType", typeof(Type), typeof(
OnEventBehavior));
32 public static readonly DependencyProperty HandleEventProperty = DependencyProperty.Register(
"HandleEvent", typeof(
bool), typeof(
OnEventBehavior));
34 private readonly RoutedEventHandler routedEventHandler;
36 private RoutedEvent routedEvent;
40 routedEventHandler = RoutedEventHandler;
46 public string EventName {
get {
return (
string)GetValue(EventNameProperty); } set { SetValue(EventNameProperty, value); } }
51 public Type EventOwnerType {
get {
return (Type)GetValue(EventOwnerTypeProperty); } set { SetValue(EventOwnerTypeProperty, value); } }
56 public bool HandleEvent {
get {
return (
bool)GetValue(HandleEventProperty); } set { SetValue(EventOwnerTypeProperty, value); } }
61 protected abstract void OnEvent();
66 if (EventName == null)
67 throw new ArgumentException(
string.Format(
"The EventName property must be set on behavior '{0}'.", GetType().FullName));
69 var eventOwnerType = EventOwnerType ?? AssociatedObject.GetType();
71 RoutedEvent[] routedEvents = EventManager.GetRoutedEvents().Where(evt => evt.Name == EventName && evt.OwnerType.IsAssignableFrom(eventOwnerType)).ToArray();
73 if (routedEvents.Length > 0)
75 if (routedEvents.Length > 1)
76 throw new NotImplementedException(
"TODO: several events found, find a way to decide the most relevant one.");
78 routedEvent = routedEvents.First();
79 AssociatedObject.AddHandler(routedEvent, routedEventHandler);
83 var eventInfo = AssociatedObject.GetType().GetEvent(EventName);
85 if (eventInfo == null)
86 throw new InvalidOperationException(
string.Format(
"Impossible to find a valid event named '{0}'.", EventName));
88 eventHandler = AnonymousEventHandler.RegisterEventHandler(eventInfo, AssociatedObject, OnEvent);
95 if (routedEvent != null)
97 AssociatedObject.RemoveHandler(routedEvent, routedEventHandler);
100 else if (eventHandler != null)
102 AnonymousEventHandler.UnregisterEventHandler(eventHandler);
An abstract behavior that allows to perform actions when an event is raised. It supports both RoutedE...
override void OnDetaching()
override void OnAttached()