Save and Restore Item Layout
- 2 minutes to read
Controls included in the Xpf.LayoutControl library provide methods to dynamically save and restore their item layouts to (from) streams or local storage.
Layout and Data Layout Control
To correctly save and then restore the layout of items, you need to assign names to the items. If names are not specified, the layout will be correctly restored, provided that the LayoutControl’s/LayoutGroup’s item collection (the order and number of items) wasn’t modified after the layout was saved.
To load a layout, we recommend using the Loaded event of a UserControl or a window.
When saving/loading layouts to a data store, only properties that can be changed by an end-user during layout customization are stored/restored.
The following example shows how to save the layout of items of a LayoutControl object to a stream, and then restore the layout. The LayoutControlBase.WriteToXML and LayoutControl.ReadFromXML methods are used to do this.
using System.Xml; using System.IO; MemoryStream stream = new MemoryStream(); //Save the layout XmlWriter writer = XmlWriter.Create(stream); myLayoutControl.WriteToXML(writer); writer.Close(); //Load the layout stream.Seek(0, SeekOrigin.Begin); XmlReader reader = XmlReader.Create(stream); myLayoutControl.ReadFromXML(reader); reader.Close();
Controls added dynamically to a Layout Control should be registered via the RegisterName method to save/restore their layout correctly.
Other controls provided by the Xpf.LayoutControl library also derive from the LayoutControlBase class and thus, can dynamically save and restore their layouts by calling the LayoutControlBase.WriteToXML and LayoutControlBase.ReadFromXML methods.
The number of settings that can be saved is limited. Typically, you can only save and restore item sizes and their orders. Global control properties (e.g, the Flow Layout Control‘s orientation or its splitters’ visibility) is not saved.