/// <summary> /// Gets the TriggerCollection containing the triggers associated with the specified object. /// </summary> /// <param name="obj">The object from which to retrieve the triggers.</param> /// <returns>A TriggerCollection containing the triggers associated with the specified object.</returns> public static TriggerCollection GetTriggers(DependencyObject obj) { TriggerCollection triggerCollection = (TriggerCollection)obj.GetValue(Interaction.TriggersProperty); if (triggerCollection == null) { triggerCollection = new TriggerCollection(); obj.SetValue(Interaction.TriggersProperty, triggerCollection); } return(triggerCollection); }
/// <exception cref="InvalidOperationException">Cannot host the same TriggerCollection on more than one object at a time.</exception> private static void OnTriggersChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { TriggerCollection oldCollection = args.OldValue as TriggerCollection; TriggerCollection newCollection = args.NewValue as TriggerCollection; if (oldCollection != newCollection) { if (oldCollection != null && ((IAttachedObject)oldCollection).AssociatedObject != null) { oldCollection.Detach(); } if (newCollection != null && obj != null) { if (((IAttachedObject)newCollection).AssociatedObject != null) { throw new InvalidOperationException("Error Message"); } newCollection.Attach(obj); } } }