windowsforms-17674-controls-and-libraries-form-layout-managers-workspace-manager.md
Many DevExpress controls provide methods to save their current layouts to a file, registry or stream, and then restore these layouts later on. These methods are sufficient when you need to manage a layout for one particular control. On the other hand, when your application contains multiple controls (toolbars, grid, navigation bar, etc.), saving/restoring the layout of each one of them manually becomes complicated. In such a case, the perfect solution would be to utilize any of the following components:
Tip
We do not recommend saving layouts to the system registry because the amount of data can be too large. Save layouts to a file or stream instead.
The Workspace Manager detects all supported DevExpress controls within the target container (form, user control) and operates their layouts as one global application layout, called workspace. The list below enumerates major DevExpress controls supported by the Workspace Manager.
If you do not want to save the layout of a specific control, call the WorkspaceManager.SetSerializationEnabled method and pass the control as a parameter:
WorkspaceManager.SetSerializationEnabled(userControl, false);
foreach (Control control in userControl.Controls) {
WorkspaceManager.SetSerializationEnabled(control, false);
if(control is GridControl) {
WorkspaceManager.SetSerializationEnabled((control as GridControl).MainView, false);
}
}
WorkspaceManager.SetSerializationEnabled(userControl, False)
For Each control As Control In userControl.Controls
WorkspaceManager.SetSerializationEnabled(control, False)
If TypeOf control Is GridControl Then
WorkspaceManager.SetSerializationEnabled((TryCast(control, GridControl)).MainView, False)
End If
Next control
You can create multiple predefined application workspaces that your users will be able to choose from, as well as save and restore their own custom workspaces.
The manager uses four methods that operate workspaces.
As you may have noticed, all of these methods work with the WorkspaceManager.Workspaces collection - the place where all active workspaces are stored. Active workspaces are those that are ready to be applied. If a workspace is stored in a file but has not been loaded yet, it is considered inactive. To add predefined workspaces to your application as active workspaces, design them first, then save to XML files and load when the application starts, for instance on the FormLoad event.
private void Form1_Load(object sender, EventArgs e) {
workspaceManager1.LoadWorkspace("Default", @"default.xml");
workspaceManager1.LoadWorkspace("Compact", @"compact_layout.xml");
workspaceManager1.LoadWorkspace("Detailed", @"full_layout.xml");
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
workspaceManager1.LoadWorkspace("Default", "default.xml")
workspaceManager1.LoadWorkspace("Compact", "compact_layout.xml")
workspaceManager1.LoadWorkspace("Detailed", "full_layout.xml")
End Sub
The manager searches for all supported controls within the object, assigned to its WorkspaceManager.TargetControl property.
The following code shows how to use the WorkspaceManager component to save the form’s bounds and state, and the layout of DevExpress controls when a form is closed, and load this layout when the form starts.
You may need to call the controls’ ForceInitialize methods (e.g., GridControl.ForceInitialize) before applying layouts to the controls in a Form.Load event handler.
string file = "layout.xml";
string workspaceName1 = "MyLayout";
private void Form1_Load(object sender, EventArgs e) {
//Use the WorkspaceManager to handle the layout of DevExpress controls that reside within the current form.
workspaceManager1.TargetControl = this;
// Save & restore the form's size, position and state along with DevExpress controls' layouts.
workspaceManager1.SaveTargetControlSettings = true;
// Disable layout load animation effects
workspaceManager1.AllowTransitionAnimation = DevExpress.Utils.DefaultBoolean.False;
// Disable (de)serialization for the following controls (if required):
//WorkspaceManager.SetSerializationEnabled(gaugeControl1, false);
//WorkspaceManager.SetSerializationEnabled(accordionControl1, false);
// When restoring layouts of controls in a Form.Load event handler,
// you may need to call the controls' ForceInitialize methods to finish their initialization before restoring their layouts.
//gridControl1.ForceInitialize();
//dockManager1.ForceInitialize();
//barManager1.ForceInitialize();
//...
//Load DevExpress controls' layouts from a file
if (workspaceManager1.LoadWorkspace(workspaceName1, file, true))
workspaceManager1.ApplyWorkspace(workspaceName1);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
//Save DevExpress controls' layouts to a file
workspaceManager1.CaptureWorkspace(workspaceName1, true);
workspaceManager1.SaveWorkspace(workspaceName1, file, true);
}
Dim file As String = "layout.xml"
Dim workspaceName1 As String = "MyLayout"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Use the WorkspaceManager to handle the layout of DevExpress controls that reside within the current form.
WorkspaceManager1.TargetControl = Me
' Save & restore the form's size, position and state along with DevExpress controls' layouts.
WorkspaceManager1.SaveTargetControlSettings = True
' Disable layout load animation effects
WorkspaceManager1.AllowTransitionAnimation = DevExpress.Utils.DefaultBoolean.False
' Disable (de)serialization for the following controls (if required):
'WorkspaceManager.SetSerializationEnabled(GaugeControl1, False)
'WorkspaceManager.SetSerializationEnabled(AccordionControl1, False)
' When restoring layouts of controls in a Form.Load event handler,
' you may need to call the controls' ForceInitialize methods to finish their initialization before restoring their layouts.
'GridControl1.ForceInitialize()
'DockManager1.ForceInitialize()
'BarManager1.ForceInitialize()
'...
' Load DevExpress controls' layouts from a file
If WorkspaceManager1.LoadWorkspace(workspaceName1, file, True) Then
WorkspaceManager1.ApplyWorkspace(workspaceName1)
End If
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
' Save DevExpress controls' layouts to a file
WorkspaceManager1.CaptureWorkspace(workspaceName1, True)
WorkspaceManager1.SaveWorkspace(workspaceName1, file, True)
End Sub
To assist both you and your end-users in creating, saving and loading workspaces, the Workspace Manager provides a bar menu, represented by the BarWorkspaceMenuItem class. This item can be added to the required Bar or RibbonControl as the image below illustrates.
By default, this item only displays the active workspaces list and a button to capture the current workspace.
You can set the button’s ShowSaveLoadCommands property to true to display additional commands, which allow your end-users to save and load workspaces at runtime.
You can use this menu when developing the application to quickly design and save to files various layout versions. Afterwards you can set the ShowSaveLoadCommands property back to its default false value.
Switching between workspaces can be followed by animation effects. You can choose from eight animation types, available out-of-the-box, by selecting them from a control’s smart-tag. To do the same in code, create the required animation and assign it to the WorkspaceManager.TransitionType property.
DevExpress.Utils.Animation.PushTransition pushTransition1 = new DevExpress.Utils.Animation.PushTransition();
workspaceManager1.TransitionType = pushTransition1;
Dim pushTransition1 As New DevExpress.Utils.Animation.PushTransition()
workspaceManager1.TransitionType = pushTransition1
View Example: How to use WorkspaceManager to capture, apply, save and load workspaces
WorkspaceManager allows you to manage serialization/deserialization of properties in the PropertySerializing and PropertyDeserializing event handlers. Check the Component and PropertyName properties to identify what settings are being stored/restored and what controls they belong to. To prevent serialization / deserialization, set Cancel to true:
private void workspaceManager1_PropertyDeserializing(object sender, DevExpress.Utils.PropertyCancelEventArgs ea)
{
GridView view = ea.Owner as GridView;
if (view != null && view == gridView1)
{
ea.Cancel = ea.PropertyName == "MultiSelect";
}
}
Private Sub workspaceManager1_PropertyDeserializing(ByVal sender As Object, ByVal ea As DevExpress.Utils.PropertyCancelEventArgs)
Dim view As GridView = TryCast(ea.Owner, GridView)
If view IsNot Nothing AndAlso view = gridView1 Then
ea.Cancel = ea.PropertyName = "MultiSelect"
End If
End Sub
See Also