Little trick with partial classes.

If you have ever look at silverlight control toolkit source you may be found that all visual states and visual state groups are stored as constants in one internal static class called visualstates.

When I have started developing my own control library I took this class and  make little improvement to it.  I decided to separate visual state and visual state groups constants in two different classes visualstates and visualstategroups, but because they are closely coupled they should not be placed far from each other so i made a little trick with partial classes to solve this problem.

Before:

   1: /// <summary>
   2: /// Names and helpers for visual states in the controls.
   3: /// </summary>
   4: internal static class VisualStates
   5: {
   7:     /// <summary>
   8:     /// Common state group.
   9:     /// </summary>
  10:     public const string GroupCommon = "CommonStates";
  11:  
  12:     /// <summary>
  13:     /// Normal state of the Common state group.
  14:     /// </summary>
  15:     public const string StateNormal = "Normal";
  16:  
  17:     /// <summary>
  18:     /// MouseOver state of the Common state group.
  19:     /// </summary>
  20:     public const string StateMouseOver = "MouseOver";
  21:  
  22:     /// <summary>
  23:     /// Pressed state of the Common state group.
  24:     /// </summary>
  25:     public const string StatePressed = "Pressed";
  26:  
  27:     /// <summary>
  28:     /// Disabled state of the Common state group.
  29:     /// </summary>
  30:     public const string StateDisabled = "Disabled";
  31:     #endregion GroupCommon

After:

   1: #region GroupCommon
   2:  
   3: /// <summary>
   4: /// Names  for visual states in the controls.
   5: /// </summary>
   6: internal static partial class VisualStates
   7: {
   8:     /// <summary>
   9:     /// Normal state of the Common state group.
  10:     /// </summary>
  11:     public const string Normal = "Normal";
  12:  
  13:     /// <summary>
  14:     /// MouseOver state of the Common state group.
  15:     /// </summary>
  16:     public const string MouseOver = "MouseOver";
  17:  
  18:     /// <summary>
  19:     /// Pressed state of the Common state group.
  20:     /// </summary>
  21:     public const string Pressed = "Pressed";
  22:  
  23:     /// <summary>
  24:     /// Disabled state of the Common state group.
  25:     /// </summary>
  26:     public const string Disabled = "Disabled";
  27: }
  28:  
  29: /// <summary>
  30: /// Names  for visual state groups in the controls.
  31: /// </summary>
  32: internal static partial class VisualStateGroups
  33: {
  34:     /// <summary>
  35:     /// Common state group.
  36:     /// </summary>
  37:     public const string Common = "CommonStates";
  38: }
  39:  
  40: #endregion GroupCommon

 

IMHO, VisualStateGroups.Common looks much better then VisualState.GroupCommon =)

No Comments