Skip to main content
RSS feed Subscribe to feed

 

Undoable Properties

The Undoable Property is the most basic kind of undoable node. It is a field where the Document Model Framework can record any change of the field.

Overview

The framework provides a single class for undoable properties:

  • UndoableProperty<T>: Manages a property that owns its value. Assignments to the property will be recorded as undoable commands.

Implementation Pattern

The following example defines a HeaderText property which holds a string.

  1. Define a Property Name for the property.
  2. Define a private field of type UndoableProperty<string>.
  3. Define a property with a
    • get accessor that reads the Value property from the undoable property
    • set accessor that sets the Value property.
  4. In the constructor of the class, create the property using the CreateProperty method.
  5. In the GetObjectData method, serialize the property using the SerializeProperty method.
  6. In the deserialization constructor, deserialize the property using the DeserializeProperty method.
[Serializable]
public sealed class Example : DocumentNode
{
    public new sealed class PropertyNames : DocumentNode.PropertyNames
    {
        public static PropertyName HeaderText = CreatePropertyName("HeaderText");
    }

    private readonly UndoableProperty<string> headerText;

    public string HeaderText
    {
        get { return headerText.Value; }
        set { headerText.Value = value; }
    }

    public Example(string text)
    {
        CreateProperty<string>(PropertyNames.HeaderText, out this.headerText, text);
    }

    protected override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        SerializeProperty(info, context, this.headerText);
    }

    internal Example(SerializationInfo info, StreamingContext context) : base(info, context)
    {
        DeserializeProperty(
           info, 
           context, 
           PropertyNames.HeaderText, 
           out this.headerText);
    }
}