VisualStates.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. namespace System.Windows.Controls
  12. {
  13. /// <summary>
  14. /// Names and helpers for visual states in the controls.
  15. /// </summary>
  16. internal static class VisualStates
  17. {
  18. #region GroupCommon
  19. /// <summary>
  20. /// Common state group.
  21. /// </summary>
  22. public const string GroupCommon = "CommonStates";
  23. /// <summary>
  24. /// Normal state of the Common state group.
  25. /// </summary>
  26. public const string StateNormal = "Normal";
  27. /// <summary>
  28. /// Normal state of the Common state group.
  29. /// </summary>
  30. public const string StateReadOnly = "ReadOnly";
  31. /// <summary>
  32. /// MouseOver state of the Common state group.
  33. /// </summary>
  34. public const string StateMouseOver = "MouseOver";
  35. /// <summary>
  36. /// Pressed state of the Common state group.
  37. /// </summary>
  38. public const string StatePressed = "Pressed";
  39. /// <summary>
  40. /// Disabled state of the Common state group.
  41. /// </summary>
  42. public const string StateDisabled = "Disabled";
  43. #endregion GroupCommon
  44. #region GroupFocus
  45. /// <summary>
  46. /// Focus state group.
  47. /// </summary>
  48. public const string GroupFocus = "FocusStates";
  49. /// <summary>
  50. /// Unfocused state of the Focus state group.
  51. /// </summary>
  52. public const string StateUnfocused = "Unfocused";
  53. /// <summary>
  54. /// Focused state of the Focus state group.
  55. /// </summary>
  56. public const string StateFocused = "Focused";
  57. #endregion GroupFocus
  58. #region GroupSelection
  59. /// <summary>
  60. /// Selection state group.
  61. /// </summary>
  62. public const string GroupSelection = "SelectionStates";
  63. /// <summary>
  64. /// Selected state of the Selection state group.
  65. /// </summary>
  66. public const string StateSelected = "Selected";
  67. /// <summary>
  68. /// Unselected state of the Selection state group.
  69. /// </summary>
  70. public const string StateUnselected = "Unselected";
  71. /// <summary>
  72. /// Selected inactive state of the Selection state group.
  73. /// </summary>
  74. public const string StateSelectedInactive = "SelectedInactive";
  75. #endregion GroupSelection
  76. #region GroupExpansion
  77. /// <summary>
  78. /// Expansion state group.
  79. /// </summary>
  80. public const string GroupExpansion = "ExpansionStates";
  81. /// <summary>
  82. /// Expanded state of the Expansion state group.
  83. /// </summary>
  84. public const string StateExpanded = "Expanded";
  85. /// <summary>
  86. /// Collapsed state of the Expansion state group.
  87. /// </summary>
  88. public const string StateCollapsed = "Collapsed";
  89. #endregion GroupExpansion
  90. #region GroupPopup
  91. /// <summary>
  92. /// Popup state group.
  93. /// </summary>
  94. public const string GroupPopup = "PopupStates";
  95. /// <summary>
  96. /// Opened state of the Popup state group.
  97. /// </summary>
  98. public const string StatePopupOpened = "PopupOpened";
  99. /// <summary>
  100. /// Closed state of the Popup state group.
  101. /// </summary>
  102. public const string StatePopupClosed = "PopupClosed";
  103. #endregion
  104. #region GroupValidation
  105. /// <summary>
  106. /// ValidationStates state group.
  107. /// </summary>
  108. public const string GroupValidation = "ValidationStates";
  109. /// <summary>
  110. /// The valid state for the ValidationStates group.
  111. /// </summary>
  112. public const string StateValid = "Valid";
  113. /// <summary>
  114. /// Invalid, focused state for the ValidationStates group.
  115. /// </summary>
  116. public const string StateInvalidFocused = "InvalidFocused";
  117. /// <summary>
  118. /// Invalid, unfocused state for the ValidationStates group.
  119. /// </summary>
  120. public const string StateInvalidUnfocused = "InvalidUnfocused";
  121. #endregion
  122. #region GroupExpandDirection
  123. /// <summary>
  124. /// ExpandDirection state group.
  125. /// </summary>
  126. public const string GroupExpandDirection = "ExpandDirectionStates";
  127. /// <summary>
  128. /// Down expand direction state of ExpandDirection state group.
  129. /// </summary>
  130. public const string StateExpandDown = "ExpandDown";
  131. /// <summary>
  132. /// Up expand direction state of ExpandDirection state group.
  133. /// </summary>
  134. public const string StateExpandUp = "ExpandUp";
  135. /// <summary>
  136. /// Left expand direction state of ExpandDirection state group.
  137. /// </summary>
  138. public const string StateExpandLeft = "ExpandLeft";
  139. /// <summary>
  140. /// Right expand direction state of ExpandDirection state group.
  141. /// </summary>
  142. public const string StateExpandRight = "ExpandRight";
  143. #endregion
  144. #region GroupHasItems
  145. /// <summary>
  146. /// HasItems state group.
  147. /// </summary>
  148. public const string GroupHasItems = "HasItemsStates";
  149. /// <summary>
  150. /// HasItems state of the HasItems state group.
  151. /// </summary>
  152. public const string StateHasItems = "HasItems";
  153. /// <summary>
  154. /// NoItems state of the HasItems state group.
  155. /// </summary>
  156. public const string StateNoItems = "NoItems";
  157. #endregion GroupHasItems
  158. #region GroupIncrease
  159. /// <summary>
  160. /// Increment state group.
  161. /// </summary>
  162. public const string GroupIncrease = "IncreaseStates";
  163. /// <summary>
  164. /// State enabled for increment group.
  165. /// </summary>
  166. public const string StateIncreaseEnabled = "IncreaseEnabled";
  167. /// <summary>
  168. /// State disabled for increment group.
  169. /// </summary>
  170. public const string StateIncreaseDisabled = "IncreaseDisabled";
  171. #endregion GroupIncrease
  172. #region GroupDecrease
  173. /// <summary>
  174. /// Decrement state group.
  175. /// </summary>
  176. public const string GroupDecrease = "DecreaseStates";
  177. /// <summary>
  178. /// State enabled for decrement group.
  179. /// </summary>
  180. public const string StateDecreaseEnabled = "DecreaseEnabled";
  181. /// <summary>
  182. /// State disabled for decrement group.
  183. /// </summary>
  184. public const string StateDecreaseDisabled = "DecreaseDisabled";
  185. #endregion GroupDecrease
  186. #region GroupIteractionMode
  187. /// <summary>
  188. /// InteractionMode state group.
  189. /// </summary>
  190. public const string GroupInteractionMode = "InteractionModeStates";
  191. /// <summary>
  192. /// Edit of the DisplayMode state group.
  193. /// </summary>
  194. public const string StateEdit = "Edit";
  195. /// <summary>
  196. /// Display of the DisplayMode state group.
  197. /// </summary>
  198. public const string StateDisplay = "Display";
  199. #endregion GroupIteractionMode
  200. #region GroupLocked
  201. /// <summary>
  202. /// DisplayMode state group.
  203. /// </summary>
  204. public const string GroupLocked = "LockedStates";
  205. /// <summary>
  206. /// Edit of the DisplayMode state group.
  207. /// </summary>
  208. public const string StateLocked = "Locked";
  209. /// <summary>
  210. /// Display of the DisplayMode state group.
  211. /// </summary>
  212. public const string StateUnlocked = "Unlocked";
  213. #endregion GroupLocked
  214. #region GroupActive
  215. /// <summary>
  216. /// Active state.
  217. /// </summary>
  218. public const string StateActive = "Active";
  219. /// <summary>
  220. /// Inactive state.
  221. /// </summary>
  222. public const string StateInactive = "Inactive";
  223. /// <summary>
  224. /// Active state group.
  225. /// </summary>
  226. public const string GroupActive = "ActiveStates";
  227. #endregion GroupActive
  228. #region GroupWatermark
  229. /// <summary>
  230. /// Non-watermarked state.
  231. /// </summary>
  232. public const string StateUnwatermarked = "Unwatermarked";
  233. /// <summary>
  234. /// Watermarked state.
  235. /// </summary>
  236. public const string StateWatermarked = "Watermarked";
  237. /// <summary>
  238. /// Watermark state group.
  239. /// </summary>
  240. public const string GroupWatermark = "WatermarkStates";
  241. #endregion GroupWatermark
  242. #region GroupCalendarButtonFocus
  243. /// <summary>
  244. /// Unfocused state for Calendar Buttons.
  245. /// </summary>
  246. public const string StateCalendarButtonUnfocused = "CalendarButtonUnfocused";
  247. /// <summary>
  248. /// Focused state for Calendar Buttons.
  249. /// </summary>
  250. public const string StateCalendarButtonFocused = "CalendarButtonFocused";
  251. /// <summary>
  252. /// CalendarButtons Focus state group.
  253. /// </summary>
  254. public const string GroupCalendarButtonFocus = "CalendarButtonFocusStates";
  255. #endregion GroupCalendarButtonFocus
  256. #region GroupBusyStatus
  257. /// <summary>
  258. /// Busy state for BusyIndicator.
  259. /// </summary>
  260. public const string StateBusy = "Busy";
  261. /// <summary>
  262. /// Idle state for BusyIndicator.
  263. /// </summary>
  264. public const string StateIdle = "Idle";
  265. /// <summary>
  266. /// Busyness group name.
  267. /// </summary>
  268. public const string GroupBusyStatus = "BusyStatusStates";
  269. #endregion
  270. #region GroupVisibility
  271. /// <summary>
  272. /// Visible state name for BusyIndicator.
  273. /// </summary>
  274. public const string StateVisible = "Visible";
  275. /// <summary>
  276. /// Hidden state name for BusyIndicator.
  277. /// </summary>
  278. public const string StateHidden = "Hidden";
  279. /// <summary>
  280. /// BusyDisplay group.
  281. /// </summary>
  282. public const string GroupVisibility = "VisibilityStates";
  283. #endregion
  284. /// <summary>
  285. /// Use VisualStateManager to change the visual state of the control.
  286. /// </summary>
  287. /// <param name="control">
  288. /// Control whose visual state is being changed.
  289. /// </param>
  290. /// <param name="useTransitions">
  291. /// A value indicating whether to use transitions when updating the
  292. /// visual state, or to snap directly to the new visual state.
  293. /// </param>
  294. /// <param name="stateNames">
  295. /// Ordered list of state names and fallback states to transition into.
  296. /// Only the first state to be found will be used.
  297. /// </param>
  298. public static void GoToState(Control control, bool useTransitions, params string[] stateNames)
  299. {
  300. Debug.Assert(control != null, "control should not be null!");
  301. Debug.Assert(stateNames != null, "stateNames should not be null!");
  302. Debug.Assert(stateNames.Length > 0, "stateNames should not be empty!");
  303. foreach (string name in stateNames)
  304. {
  305. if (VisualStateManager.GoToState(control, name, useTransitions))
  306. {
  307. break;
  308. }
  309. }
  310. }
  311. /// <summary>
  312. /// Gets the implementation root of the Control.
  313. /// </summary>
  314. /// <param name="dependencyObject">The DependencyObject.</param>
  315. /// <remarks>
  316. /// Implements Silverlight's corresponding internal property on Control.
  317. /// </remarks>
  318. /// <returns>Returns the implementation root or null.</returns>
  319. public static FrameworkElement GetImplementationRoot(DependencyObject dependencyObject)
  320. {
  321. Debug.Assert(dependencyObject != null, "DependencyObject should not be null.");
  322. return (1 == VisualTreeHelper.GetChildrenCount(dependencyObject)) ?
  323. VisualTreeHelper.GetChild(dependencyObject, 0) as FrameworkElement :
  324. null;
  325. }
  326. /// <summary>
  327. /// This method tries to get the named VisualStateGroup for the
  328. /// dependency object. The provided object's ImplementationRoot will be
  329. /// looked up in this call.
  330. /// </summary>
  331. /// <param name="dependencyObject">The dependency object.</param>
  332. /// <param name="groupName">The visual state group's name.</param>
  333. /// <returns>Returns null or the VisualStateGroup object.</returns>
  334. public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
  335. {
  336. FrameworkElement root = GetImplementationRoot(dependencyObject);
  337. if (root == null)
  338. {
  339. return null;
  340. }
  341. return VisualStateManager.GetVisualStateGroups(root)
  342. .OfType<VisualStateGroup>()
  343. .Where(group => string.CompareOrdinal(groupName, group.Name) == 0)
  344. .FirstOrDefault();
  345. }
  346. }
  347. }