Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BehaviorCollection.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.ObjectModel;
5 using System.Collections.Specialized;
6 using System.Windows;
7 using System.Windows.Interactivity;
8 
9 namespace SiliconStudio.Presentation.Interactivity
10 {
11  /// <summary>
12  /// A collection of behavior that synchronize with the System.Windows.Interactivity.Interaction.Behaviors attached property.
13  /// </summary>
14  public class BehaviorCollection : ObservableCollection<Behavior>, IAttachedObject
15  {
17  {
18  var collection = (INotifyCollectionChanged)this;
19  collection.CollectionChanged += BehaviorCollectionChanged;
20  }
21 
22  public DependencyObject AssociatedObject { get; private set; }
23 
25  {
26  var clone = new BehaviorCollection();
27  foreach (var behavior in Items)
28  {
29  clone.Add((Behavior)behavior.Clone());
30  }
31  return clone;
32  }
33 
34  public void Attach(DependencyObject dependencyObject)
35  {
36  if (dependencyObject == null) throw new ArgumentNullException("dependencyObject");
37  // Aleady attached
38  if (ReferenceEquals(AssociatedObject, dependencyObject))
39  return;
40 
41  if (AssociatedObject != null)
42  throw new InvalidOperationException("This BehaviorCollection has already been attached to a dependency object.");
43 
44  AssociatedObject = dependencyObject;
45  var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(dependencyObject);
46  foreach (Behavior behavior in this)
47  {
48  behaviors.Add(behavior);
49  }
50  }
51 
52  public void Detach()
53  {
54  if (AssociatedObject != null)
55  {
56  var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(AssociatedObject);
57  foreach (Behavior behavior in this)
58  {
59  behaviors.Remove(behavior);
60  }
61  }
62  AssociatedObject = null;
63  }
64 
65  private void BehaviorCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
66  {
67  if (AssociatedObject == null)
68  return;
69 
70  var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(AssociatedObject);
71  switch (e.Action)
72  {
73  case NotifyCollectionChangedAction.Add:
74  foreach (Behavior newItem in e.NewItems)
75  behaviors.Add(newItem);
76  break;
77  case NotifyCollectionChangedAction.Remove:
78  foreach (Behavior oldItem in e.OldItems)
79  behaviors.Remove(oldItem);
80  break;
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);
86  break;
87  case NotifyCollectionChangedAction.Reset:
88  behaviors.Clear();
89  foreach (Behavior newItem in e.NewItems)
90  behaviors.Add(newItem);
91  break;
92  default:
93  throw new ArgumentOutOfRangeException();
94  }
95  }
96  }
97 }
A collection of behavior that synchronize with the System.Windows.Interactivity.Interaction.Behaviors attached property.