1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.State
7 using System.Diagnostics;
8 using System.Diagnostics.CodeAnalysis;
12 // Simple state object that knows how to remember state for a single property.
13 // The only thing it currently remembers is whether the property's sub-properties
14 // are expanded or collapsed.
16 internal class PropertyState : PersistedState
19 private const bool DefaultSubPropertiesExpanded = false;
21 private string _propertyName;
22 private bool _subPropertiesExpanded = DefaultSubPropertiesExpanded;
27 // <param name="propertyName">Property to wrap around</param>
28 [SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily")]
29 public PropertyState(string propertyName)
31 Fx.Assert(!string.IsNullOrEmpty(propertyName), "Expected a full property name");
32 _propertyName = propertyName;
36 // We key these state objects by their property names
38 public override object Key
40 get { return _propertyName; }
44 // Returns true if any of the contained values differ from the default
46 public override bool IsSignificant
48 get { return _subPropertiesExpanded != DefaultSubPropertiesExpanded; }
52 // Gets or sets a flag indicating whether the sub-properties of the contained
53 // property have been expanded or collapsed.
55 public bool SubPropertiesExpanded
57 get { return _subPropertiesExpanded; }
58 set { _subPropertiesExpanded = value; }
62 // Serializes this object into a simple string (AppDomains like strings).
64 // Format: PropertyName,SubPropertiesExpanded;NextPropertyName,SubPropertiesExpanded;...
65 // Where bools are recorded as 0 = false and 1 = true
67 // <returns>Serialized version of this state object (may be null)</returns>
68 protected override string SerializeCore()
71 PersistedStateUtilities.Escape(_propertyName),
73 PersistedStateUtilities.BoolToDigit(_subPropertiesExpanded));
77 // Attempts to deserialize a string into a PropertyState object
79 // <param name="propertyStateString">String to deserialize</param>
80 // <returns>Instance of PropertyState if the serialized string was valid, null otherwise.</returns>
81 public static PropertyState Deserialize(string propertyStateString)
83 string[] args = propertyStateString.Split(',');
84 if (args == null || args.Length != 2)
89 bool? subPropertiesExpanded = PersistedStateUtilities.DigitToBool(args[1]);
90 if (subPropertiesExpanded == null)
95 string propertyName = PersistedStateUtilities.Unescape(args[0]);
96 if (string.IsNullOrEmpty(propertyName))
101 PropertyState propertyState = new PropertyState(propertyName);
102 propertyState.SubPropertiesExpanded = (bool)subPropertiesExpanded;
103 return propertyState;