internal static bool TryGetState(IList <VisualStateGroup> groups, string stateName, out VisualStateGroup group, out VisualState state) { for (int groupIndex = 0; groupIndex < groups.Count; ++groupIndex) { VisualStateGroup g = groups[groupIndex]; VisualState s = g.GetState(stateName); if (s != null) { group = g; state = s; return(true); } } group = null; state = null; return(false); }
/// <summary> /// Get the most appropriate transition between two states. /// </summary> /// <param name="element">Element being transitioned.</param> /// <param name="group">Group being transitioned.</param> /// <param name="from">VisualState being transitioned from.</param> /// <param name="to">VisualState being transitioned to.</param> /// <returns> /// The most appropriate transition between the desired states. /// </returns> internal static VisualTransition GetTransition(FrameworkElement element, VisualStateGroup group, VisualState from, VisualState to) { if (element == null) { throw new ArgumentNullException("element"); } if (group == null) { throw new ArgumentNullException("group"); } if (to == null) { throw new ArgumentNullException("to"); } VisualTransition best = null; VisualTransition defaultTransition = null; int bestScore = -1; IList <VisualTransition> transitions = (IList <VisualTransition>)group.Transitions; if (transitions != null) { foreach (VisualTransition transition in transitions) { if (defaultTransition == null && transition.IsDefault) { defaultTransition = transition; continue; } int score = -1; VisualState transitionFromState = group.GetState(transition.From); VisualState transitionToState = group.GetState(transition.To); if (from == transitionFromState) { score += 1; } else if (transitionFromState != null) { continue; } if (to == transitionToState) { score += 2; } else if (transitionToState != null) { continue; } if (score > bestScore) { bestScore = score; best = transition; } } } return(best ?? defaultTransition); }