Skip to main content
A newer version of this page is available. .

LayoutSerializationService

  • 7 minutes to read

LayoutSerializationService is an ILayoutSerializationService implementation that allows you to save/restore layout of serializable DevExpress WPF Controls located on a View.(i.e., DXGrid, DXPivotGrid, DXDocking, DXBars and DXLayoutControl).

Getting Started with the LayoutSerializationService

To save/restore a layout of your View and its child element, add the LayoutSerializationService to the View’s dxmvvm:Interaction.Behaviors collection.

<UserControl x:Class="LayoutSerializationService.Views.MainView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:LayoutSerializationService.ViewModels"
    DataContext="{dxmvvm:ViewModelSource Type=ViewModels:MainViewModel}">
    ...
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:LayoutSerializationService />
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>

Then, use one of the following ways to access the defined service.

Next, call the service’s Serialize/Deserialize method to save/restore the View’s layout respectively. The Serialize method returns a string object that contains layout settings that you can restore by using the Deserialize method in the sequel. To restore a specific layout, pass the corresponding string object to the Deserialize method using its parameter.

When the Serialize/Deserialize method is invoked, the LayoutSerializationService iterates through visual tree elements and saves/restores the layout of controls supporting serialization and their child elements by using the DevExpress.Xpf.Core.Serialization.DXSerializer methods. Therefore, to properly perform serialization and deserialization, follow the recommendations described in the Saving and Restoring Layout Basics thread.

Tip

If you use LayoutSerializationService in conjunction with CurrentWindowSerializationBehavior, use the Initialized event to deserialize the layout on the application startup. Otherwise, the window may flicker during the deserialization.

Serialization Capabilities

To prevent a specific control and its child elements from being serialized/deserialized, set the attached dx:DXSerializer.Enabled property to False for this control.

Example

using System;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DocumentManagerSerialization.Properties;
using System.Collections.Generic;
using DocumentManagerSerialization.Common;

namespace DocumentManagerSerialization.ViewModels {
    [POCOViewModel]
    public class MainViewModel : ISupportLogicalLayout {

        public IDocumentManagerService DocumentManagerService { get { return this.GetService<IDocumentManagerService>(); } }
        public ILayoutSerializationService LayoutSerializationService { get { return this.GetService<ILayoutSerializationService>(); } }
        public virtual ViewModelState State { get; set; }

        public MainViewModel() {
            State = new ViewModelState() { State = "I'm Root!" };
        }

        [Command]
        public void OnWindowClosing() {
            Settings.Default.LogicalLayout = this.SerializeDocumentManagerService();
            Settings.Default.RootLayout = LayoutSerializationService.Serialize();
            Settings.Default.Save();
        }

        [Command]
        public void OnWindowLoaded() {
            if (Settings.Default.LogicalLayout != null) {
                this.RestoreDocumentManagerService(Settings.Default.LogicalLayout);
            }
            if (Settings.Default.RootLayout != null) {
                LayoutSerializationService.Deserialize(Settings.Default.RootLayout);
            }
        }

        [Command]
        public void OpenDocument() {
            var document = DocumentManagerService.CreateDocument("DocumentView", null, this);
            document.Id = "Document" + Guid.NewGuid().ToString().Replace("-", "");
            document.DestroyOnClose = false;
            document.Title = "Root Document";
            document.Show();
        }

        #region ISupportLogicalLayout
        public bool CanSerialize {
            get { return true; }
        }

        public IEnumerable<object> LookupViewModels {
            get { return null; }
        }
        #endregion
    }
}
See Also