4 using System.Collections.ObjectModel;
5 using System.Collections.Specialized;
7 using System.Windows.Interactivity;
9 namespace SiliconStudio.Presentation.Interactivity
19 collection.CollectionChanged += BehaviorCollectionChanged;
27 foreach (var behavior
in Items)
29 clone.Add((Behavior)behavior.Clone());
36 if (dependencyObject == null)
throw new ArgumentNullException(
"dependencyObject");
38 if (ReferenceEquals(AssociatedObject, dependencyObject))
41 if (AssociatedObject != null)
42 throw new InvalidOperationException(
"This BehaviorCollection has already been attached to a dependency object.");
44 AssociatedObject = dependencyObject;
45 var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(dependencyObject);
46 foreach (Behavior behavior
in this)
48 behaviors.Add(behavior);
54 if (AssociatedObject != null)
56 var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(AssociatedObject);
57 foreach (Behavior behavior
in this)
59 behaviors.Remove(behavior);
62 AssociatedObject = null;
65 private void BehaviorCollectionChanged(
object sender, NotifyCollectionChangedEventArgs e)
67 if (AssociatedObject == null)
70 var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(AssociatedObject);
73 case NotifyCollectionChangedAction.Add:
74 foreach (Behavior newItem
in e.NewItems)
75 behaviors.Add(newItem);
77 case NotifyCollectionChangedAction.Remove:
78 foreach (Behavior oldItem
in e.OldItems)
79 behaviors.Remove(oldItem);
81 case NotifyCollectionChangedAction.Replace:
82 foreach (Behavior oldItem
in e.OldItems)
83 behaviors.Remove(oldItem);
84 foreach (Behavior newItem
in e.NewItems)
85 behaviors.Add(newItem);
87 case NotifyCollectionChangedAction.Reset:
89 foreach (Behavior newItem
in e.NewItems)
90 behaviors.Add(newItem);
93 throw new ArgumentOutOfRangeException();
void Attach(DependencyObject dependencyObject)
A collection of behavior that synchronize with the System.Windows.Interactivity.Interaction.Behaviors attached property.
BehaviorCollection Clone()