private static int GetIndexOf(System.Windows.Interactivity.BehaviorCollection itemBehaviors, Behavior behavior) { int index = -1; Behavior orignalBehavior = GetOriginalBehavior(behavior); for (int i = 0; i < itemBehaviors.Count; i++) { Behavior currentBehavior = itemBehaviors[i]; if (currentBehavior == behavior || currentBehavior == orignalBehavior) { index = i; break; } Behavior currentOrignalBehavior = GetOriginalBehavior(currentBehavior); if (currentOrignalBehavior == behavior || currentOrignalBehavior == orignalBehavior) { index = i; break; } } return(index); }
private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DependencyObject target = d; if (e.OldValue != null) { InteractivityTemplate interactivityTemplate = (InteractivityTemplate)e.OldValue; InteractivityItems interactivityItems = (InteractivityItems)interactivityTemplate.LoadContent(); BehaviorCollection behaviorCollection = Interaction.GetBehaviors(target); TriggerCollection triggerCollection = Interaction.GetTriggers(target); interactivityItems.Behaviors.ForEach(x => behaviorCollection.Remove(x)); interactivityItems.Triggers.ForEach(x => triggerCollection.Remove(x)); } if (e.NewValue != null) { InteractivityTemplate interactivityTemplate = (InteractivityTemplate)e.NewValue; interactivityTemplate.Seal(); InteractivityItems interactivityItems = (InteractivityItems)interactivityTemplate.LoadContent(); BehaviorCollection behaviorCollection = Interaction.GetBehaviors(target); TriggerCollection triggerCollection = Interaction.GetTriggers(target); interactivityItems.Behaviors.ForEach(behaviorCollection.Add); interactivityItems.Triggers.ForEach(triggerCollection.Add); } }
private static void OnBehaviorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { System.Windows.Interactivity.BehaviorCollection behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(d); foreach (System.Windows.Interactivity.Behavior behavior in e.NewValue as StyleBehaviorCollection) { behaviors.Add((System.Windows.Interactivity.Behavior)behavior.Clone()); } }
/// <summary> /// Gets the System.Windows.Interactivity.BehaviorCollection associated with /// a specified object. /// </summary> /// <param name="obj">The object from which to retrieve the System.Windows.Interactivity.BehaviorCollection.</param> /// <returns> /// A System.Windows.Interactivity.BehaviorCollection containing the behaviors /// associated with the specified object. /// </returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { if (obj.GetValue(BehaviorsProperty) == null) { BehaviorCollection col = new BehaviorCollection(); col.Attach(obj); obj.SetValue(BehaviorsProperty, col); } return((BehaviorCollection)obj.GetValue(BehaviorsProperty)); }
/// <summary> /// Get the behavior list /// </summary> /// <param name="obj">Object to retrieve behavior collection from</param> /// <returns>Behavior collection (empty if none)</returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { var coll = (BehaviorCollection)obj.GetValue(BehaviorsProperty); if (coll == null) { coll = new BehaviorCollection(); SetBehaviors(obj, coll); } return(coll); }
public static BehaviorCollection GetBehaviors(DependencyObject obj) { BehaviorCollection value = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty); if (value == null) { value = new BehaviorCollection(); obj.SetValue(Interaction.BehaviorsProperty, value); } return(value); }
private static void OnBehaviorCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var uie = d as UIElement; if (uie == null) { return; } System.Windows.Interactivity.BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie); var newBehaviors = e.NewValue as BehaviorCollection; var oldBehaviors = e.OldValue as BehaviorCollection; if (newBehaviors == oldBehaviors) { return; } if (oldBehaviors != null) { foreach (var behavior in oldBehaviors) { int index = GetIndexOf(itemBehaviors, behavior); if (index >= 0) { itemBehaviors.RemoveAt(index); } } } if (newBehaviors != null) { foreach (var behavior in newBehaviors) { int index = GetIndexOf(itemBehaviors, behavior); if (index < 0) { var clone = (Behavior)behavior.Clone(); SetOriginalBehavior(clone, behavior); itemBehaviors.Add(clone); } } } }
/// <summary> /// This is called when the behavior collection is changed on an object. /// </summary> /// <param name="dpo">FrameworkElement owner</param> /// <param name="e"></param> private static void OnBehaviorListChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e) { BehaviorCollection list = e.OldValue as BehaviorCollection; if (list != null) { list.Detach(); } list = e.NewValue as BehaviorCollection; FrameworkElement fe = dpo as FrameworkElement; if (list != null && fe != null) { list.Attach(fe); } }
private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { BehaviorCollection oldValue = (BehaviorCollection)args.OldValue; BehaviorCollection newValue = (BehaviorCollection)args.NewValue; if (oldValue != newValue) { if (oldValue != null && ((IAttachedObject)oldValue).AssociatedObject != null) { oldValue.Detach(); } if (newValue != null && obj != null) { if (((IAttachedObject)newValue).AssociatedObject != null) { throw new InvalidOperationException(ExceptionStringTable.CannotHostBehaviorCollectionMultipleTimesExceptionMessage); } newValue.Attach(obj); } } }
private static void OnTemplateChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { InteractivityTemplate dt = (InteractivityTemplate)e.NewValue; #if (!SILVERLIGHT) dt.Seal(); #endif InteractivityItems ih = (InteractivityItems)dt.LoadContent(); BehaviorCollection bc = Interaction.GetBehaviors(d); TriggerCollection tc = Interaction.GetTriggers(d); foreach (Behavior behavior in ih.Behaviors) { bc.Add(behavior); } foreach (TriggerBase trigger in ih.Triggers) { tc.Add(trigger); } }
/// <summary> /// Sets the behavior list. /// </summary> /// <param name="obj">Object to set behavior collection on</param> /// <param name="value">Collection to assign</param> public static void SetBehaviors(DependencyObject obj, BehaviorCollection value) { obj.SetValue(BehaviorsProperty, value); }