windowsforms-117283-common-features-behaviors-persistence-behavior.md
Use PersistenceBehavior to automatically save and restore a form’s bounds and its state to/from a file or the system registry. Along with the form-related information, it can optionally save/restore the layout of all DevExpress controls/components that reside within this form.
You can also bind PersistenceBehavior to an individual serializable DevExpress control/component to automatically save/restore only its layout.
Note
Use Workspace Manager to manually save/restore the layout of DevExpress controls/components, and the form’s bounds and state when necessary. The Workspace Manager provides additional features:
The following animation illustrates a form with attached PersistenceBehavior.
Drop the BehaviorManager component on the form. Invoke the Behavior Editor from the smart tag menu and add PersistenceBehavior.
Ensure the Target property is set to the form.
Optionally set the StoreChildLayouts setting to True to save/restore the layouts of all serializable DevExpress components within this form.
Use the Storage property to specify the storage type (the system registry or a XML file).
Set the Path property to a target system registry key or the path to a target XML file.
When you need to create PersistenceBehavior in code, do so in the form’s constructor after the InitializeComponent method call. The following code shows how to create PersistenceBehavior in code to save/restore the layout of the form and DevExpress controls in this form. The example excludes a GaugeControl from (de)serialization.
using DevExpress.Utils.Behaviors.Common;
public Form1() {
InitializeComponent();
behaviorManager1.Attach<PersistenceBehavior>(this, behavior => {
behavior.Properties.StoreChildLayouts = DefaultBoolean.True;
behavior.Properties.Storage = Storage.File;
behavior.Properties.Path = "mylayout.xml";
//save the layout to the system registry:
//behavior.Properties.Storage = Storage.Registry;
//behavior.Properties.Path = "HKEY_CURRENT_USER\\Software\\MyCompany\\MyProject\\Layout";
});
//Disable layout (de)serialization for a GaugeControl
PersistenceBehavior.SetSerializationEnabled(gaugeControl1, false);
}
Imports DevExpress.Utils.Behaviors.Common
Public Sub New()
InitializeComponent()
BehaviorManager1.Attach(Of PersistenceBehavior)(Me, Sub(behavior)
behavior.Properties.StoreChildLayouts = DefaultBoolean.True
behavior.Properties.Storage = Storage.File
behavior.Properties.Path = "mylayout.xml"
'save the layout to the system registry:
'behavior.Properties.Storage = Storage.Registry
'behavior.Properties.Path = "HKEY_CURRENT_USER\Software\MyCompany\MyProject\Layout"
End Sub)
'Disable layout (de)serialization for a GaugeControl
PersistenceBehavior.SetSerializationEnabled(GaugeControl1, False)
End Sub
Binding to Form Specifics
PersistenceBehavior bound to a form saves the layout when the Form.FormClosed event fires, and it restores the saved layout when the Form.Load event fires.
Ensure safe layout deserialization
If you create DevExpress controls/components at design time, call the controls’ ForceInitialize methods (e.g., BarManager.ForceInitialize, DockManager.ForceInitialize and GridControl.ForceInitialize) before you restore control layouts during form initialization. These methods finish control initialization and ensure you can safely modify their settings (e.g., by loading a layout). The following example calls the RibbonControl.ForceInitialize method before the Form.Load event by overriding the form’s OnLoad method.
public partial class Form1 : Form {
protected override void OnLoad(EventArgs e) {
ribbonControl1.ForceInitialize();
base.OnLoad(e);
}
}
Partial Public Class Form1
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
RibbonControl1.ForceInitialize()
MyBase.OnLoad(e)
End Sub
End Class
You can save/restore a specific control’s layout using the SaveLayoutTo… and RestoreLayoutFrom… methods the control provides. You can also use PersistenceBehavior for the same purpose.
Target controls and components include: DockManager, GridControl, TreeList, VGridControl, LayoutControl, DocumentManager‘s Views, etc.
Binding to Control specifics
When bound to a control, PersistenceBehavior handles the Control.HandleCreated and Control.HandleDestroyed events to automatically restore/save the control’s layout.
Binding to Component specifics
When bound to a component (e.g., DockManager, DocumentManager‘s Views), PersistenceBehavior saves/restores the layout when the owner form is closed and loaded (the Form.FormClosing and Form.Load events, respectively). If you set the PersistenceBehavior.StoreChildLayouts to True , PersistenceBehavios saves/restores the layout of all serializable controls within the owner form.
public Form1() {
InitializeComponent();
behaviorManager1.Attach<PersistenceBehavior>(dockManager1, behavior => {
behavior.Properties.Storage = Storage.File;
behavior.Properties.Path = "mylayout.xml";
});
}
Public Sub New()
InitializeComponent()
BehaviorManager1.Attach(Of PersistenceBehavior)(DockManager1, Sub(behavior)
behavior.Properties.Storage = Storage.File
behavior.Properties.Path = "mylayout.xml"
End Sub)
End Sub
DevExpress controls provide settings to customize the layout save/restore operations. These settings allow you to specify:
The list below shows the objects used to access the layout-aware settings.
When you load a layout, you can perform additional layout customization by handling the LayoutUpgrade deserialization events.
To prevent a layout from being loaded, you can handle the BeforeLoadLayout events.