// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
// All other rights reserved.
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace System.Windows.Controls
{
///
/// Names and helpers for visual states in the controls.
///
internal static class VisualStates
{
#region GroupCommon
///
/// Common state group.
///
public const string GroupCommon = "CommonStates";
///
/// Normal state of the Common state group.
///
public const string StateNormal = "Normal";
///
/// Normal state of the Common state group.
///
public const string StateReadOnly = "ReadOnly";
///
/// MouseOver state of the Common state group.
///
public const string StateMouseOver = "MouseOver";
///
/// Pressed state of the Common state group.
///
public const string StatePressed = "Pressed";
///
/// Disabled state of the Common state group.
///
public const string StateDisabled = "Disabled";
#endregion GroupCommon
#region GroupFocus
///
/// Focus state group.
///
public const string GroupFocus = "FocusStates";
///
/// Unfocused state of the Focus state group.
///
public const string StateUnfocused = "Unfocused";
///
/// Focused state of the Focus state group.
///
public const string StateFocused = "Focused";
#endregion GroupFocus
#region GroupSelection
///
/// Selection state group.
///
public const string GroupSelection = "SelectionStates";
///
/// Selected state of the Selection state group.
///
public const string StateSelected = "Selected";
///
/// Unselected state of the Selection state group.
///
public const string StateUnselected = "Unselected";
///
/// Selected inactive state of the Selection state group.
///
public const string StateSelectedInactive = "SelectedInactive";
#endregion GroupSelection
#region GroupExpansion
///
/// Expansion state group.
///
public const string GroupExpansion = "ExpansionStates";
///
/// Expanded state of the Expansion state group.
///
public const string StateExpanded = "Expanded";
///
/// Collapsed state of the Expansion state group.
///
public const string StateCollapsed = "Collapsed";
#endregion GroupExpansion
#region GroupPopup
///
/// Popup state group.
///
public const string GroupPopup = "PopupStates";
///
/// Opened state of the Popup state group.
///
public const string StatePopupOpened = "PopupOpened";
///
/// Closed state of the Popup state group.
///
public const string StatePopupClosed = "PopupClosed";
#endregion
#region GroupValidation
///
/// ValidationStates state group.
///
public const string GroupValidation = "ValidationStates";
///
/// The valid state for the ValidationStates group.
///
public const string StateValid = "Valid";
///
/// Invalid, focused state for the ValidationStates group.
///
public const string StateInvalidFocused = "InvalidFocused";
///
/// Invalid, unfocused state for the ValidationStates group.
///
public const string StateInvalidUnfocused = "InvalidUnfocused";
#endregion
#region GroupExpandDirection
///
/// ExpandDirection state group.
///
public const string GroupExpandDirection = "ExpandDirectionStates";
///
/// Down expand direction state of ExpandDirection state group.
///
public const string StateExpandDown = "ExpandDown";
///
/// Up expand direction state of ExpandDirection state group.
///
public const string StateExpandUp = "ExpandUp";
///
/// Left expand direction state of ExpandDirection state group.
///
public const string StateExpandLeft = "ExpandLeft";
///
/// Right expand direction state of ExpandDirection state group.
///
public const string StateExpandRight = "ExpandRight";
#endregion
#region GroupHasItems
///
/// HasItems state group.
///
public const string GroupHasItems = "HasItemsStates";
///
/// HasItems state of the HasItems state group.
///
public const string StateHasItems = "HasItems";
///
/// NoItems state of the HasItems state group.
///
public const string StateNoItems = "NoItems";
#endregion GroupHasItems
#region GroupIncrease
///
/// Increment state group.
///
public const string GroupIncrease = "IncreaseStates";
///
/// State enabled for increment group.
///
public const string StateIncreaseEnabled = "IncreaseEnabled";
///
/// State disabled for increment group.
///
public const string StateIncreaseDisabled = "IncreaseDisabled";
#endregion GroupIncrease
#region GroupDecrease
///
/// Decrement state group.
///
public const string GroupDecrease = "DecreaseStates";
///
/// State enabled for decrement group.
///
public const string StateDecreaseEnabled = "DecreaseEnabled";
///
/// State disabled for decrement group.
///
public const string StateDecreaseDisabled = "DecreaseDisabled";
#endregion GroupDecrease
#region GroupIteractionMode
///
/// InteractionMode state group.
///
public const string GroupInteractionMode = "InteractionModeStates";
///
/// Edit of the DisplayMode state group.
///
public const string StateEdit = "Edit";
///
/// Display of the DisplayMode state group.
///
public const string StateDisplay = "Display";
#endregion GroupIteractionMode
#region GroupLocked
///
/// DisplayMode state group.
///
public const string GroupLocked = "LockedStates";
///
/// Edit of the DisplayMode state group.
///
public const string StateLocked = "Locked";
///
/// Display of the DisplayMode state group.
///
public const string StateUnlocked = "Unlocked";
#endregion GroupLocked
#region GroupActive
///
/// Active state.
///
public const string StateActive = "Active";
///
/// Inactive state.
///
public const string StateInactive = "Inactive";
///
/// Active state group.
///
public const string GroupActive = "ActiveStates";
#endregion GroupActive
#region GroupWatermark
///
/// Non-watermarked state.
///
public const string StateUnwatermarked = "Unwatermarked";
///
/// Watermarked state.
///
public const string StateWatermarked = "Watermarked";
///
/// Watermark state group.
///
public const string GroupWatermark = "WatermarkStates";
#endregion GroupWatermark
#region GroupCalendarButtonFocus
///
/// Unfocused state for Calendar Buttons.
///
public const string StateCalendarButtonUnfocused = "CalendarButtonUnfocused";
///
/// Focused state for Calendar Buttons.
///
public const string StateCalendarButtonFocused = "CalendarButtonFocused";
///
/// CalendarButtons Focus state group.
///
public const string GroupCalendarButtonFocus = "CalendarButtonFocusStates";
#endregion GroupCalendarButtonFocus
#region GroupBusyStatus
///
/// Busy state for BusyIndicator.
///
public const string StateBusy = "Busy";
///
/// Idle state for BusyIndicator.
///
public const string StateIdle = "Idle";
///
/// Busyness group name.
///
public const string GroupBusyStatus = "BusyStatusStates";
#endregion
#region GroupVisibility
///
/// Visible state name for BusyIndicator.
///
public const string StateVisible = "Visible";
///
/// Hidden state name for BusyIndicator.
///
public const string StateHidden = "Hidden";
///
/// BusyDisplay group.
///
public const string GroupVisibility = "VisibilityStates";
#endregion
///
/// Use VisualStateManager to change the visual state of the control.
///
///
/// Control whose visual state is being changed.
///
///
/// A value indicating whether to use transitions when updating the
/// visual state, or to snap directly to the new visual state.
///
///
/// Ordered list of state names and fallback states to transition into.
/// Only the first state to be found will be used.
///
public static void GoToState(Control control, bool useTransitions, params string[] stateNames)
{
Debug.Assert(control != null, "control should not be null!");
Debug.Assert(stateNames != null, "stateNames should not be null!");
Debug.Assert(stateNames.Length > 0, "stateNames should not be empty!");
foreach (string name in stateNames)
{
if (VisualStateManager.GoToState(control, name, useTransitions))
{
break;
}
}
}
///
/// Gets the implementation root of the Control.
///
/// The DependencyObject.
///
/// Implements Silverlight's corresponding internal property on Control.
///
/// Returns the implementation root or null.
public static FrameworkElement GetImplementationRoot(DependencyObject dependencyObject)
{
Debug.Assert(dependencyObject != null, "DependencyObject should not be null.");
return (1 == VisualTreeHelper.GetChildrenCount(dependencyObject)) ?
VisualTreeHelper.GetChild(dependencyObject, 0) as FrameworkElement :
null;
}
///
/// This method tries to get the named VisualStateGroup for the
/// dependency object. The provided object's ImplementationRoot will be
/// looked up in this call.
///
/// The dependency object.
/// The visual state group's name.
/// Returns null or the VisualStateGroup object.
public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
{
FrameworkElement root = GetImplementationRoot(dependencyObject);
if (root == null)
{
return null;
}
return VisualStateManager.GetVisualStateGroups(root)
.OfType()
.Where(group => string.CompareOrdinal(groupName, group.Name) == 0)
.FirstOrDefault();
}
}
}