Skip to main content
All docs
V24.2

CurrentWindowSerializationBehavior Class

Allows you to serialize/deserialize settings (size and state) of the associated view (or window).

Namespace: DevExpress.Mvvm.UI

Assembly: DevExpress.Xpf.Core.v24.2.dll

NuGet Package: DevExpress.Wpf.Core

Declaration

public class CurrentWindowSerializationBehavior :
    Behavior<DependencyObject>

Remarks

The CurrentWindowSerializationBehavior uses the LayoutSerializationService to serialize/deserialize a view’s settings.

Follow the steps below to serialize/deserialize view settings:

  1. Assign the LayoutSerializationService to the view.
  2. Define the CurrentWindowSerializationBehavior at the same hierarchical level with the LayoutSerializationService or lower.
  3. Specify the commands that call the LayoutSerializationService.Serialize and LayoutSerializationService.Deserialize methods and bind these commands to a control’s Command property.

The following code sample serializes/deserializes the MainView’s settings (size and state) on a button click:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:LayoutSerializationService.ViewModels">
    <UserControl.DataContext>
        <ViewModels.MainViewModel>
    </UserControl.DataContext>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CurrentWindowSerializationBehavior/>
        <dxmvvm:LayoutSerializationService/>
    </dxmvvm:Interaction.Behaviors>
    <StackPanel>
        <!-- ... -->
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <Button Command="{Binding SaveLayoutCommand}" Content="Save Layout" />
            <Button Command="{Binding LoadLayoutCommand}" Content="Load Layout" />
        </StackPanel>
    </StackPanel>
</UserControl>
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using System.Collections.Generic;
using DocumentManagerSerialization.Common;

// ...
[POCOViewModel]
public class MainViewModel : ISupportLogicalLayout {
    public ILayoutSerializationService LayoutSerializationService { get { return this.GetService<ILayoutSerializationService>(); } }

    public virtual ViewModelState State { get; set; }

    public MainViewModel() {
        State = new ViewModelState() { State = "Initialized" };
    }

    [Command]
    public void SaveLayout() {
        State.State = LayoutSerializationService.Serialize();
    }

    [Command]
    public void LoadLayout() {
        LayoutSerializationService.Deserialize(State.State);
    }
}

Serialize/Deserialize a View’s Settings On Application Startup/Shutdown

  1. Assign the LayoutSerializationService to the view.
  2. Define the CurrentWindowSerializationBehavior at the same hierarchical level with the LayoutSerializationService or lower.
  3. Create a ViewModel’s command that serializes the view’s settings. In the ViewModel’s command, call the LayoutSerializationService.Deserialize method to deserialize view settings.
  4. Create a ViewModel’s commands that deserializes the view’s settings. In the ViewModel’s command, call the LayoutSerializationService.Serialize to serialize view settings when the application shuts down.
  5. Use the EventToCommand behavior to bind the ViewModel’s command to the Initialized event. In this case, the application executes the command on application startup.
  6. Use the CurrentWindowService to bind the ViewModel’s command to the Unloaded event. In this case, the application executes the command when the application shuts down.

The following code sample serializes/deserializes the MainView’s settings (size and state) on application close/startup:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:LayoutSerializationService.ViewModels">
    <UserControl.DataContext>
        <ViewModels.MainViewModel>
    </UserControl.DataContext>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CurrentWindowSerializationBehavior/>
        <dxmvvm:LayoutSerializationService/>
        <dxmvvm:CurrentWindowService ClosingCommand="{Binding OnWindowClosingCommand}" />
        <dxmvvm:EventToCommand Command="{Binding OnWindowLoadedCommand}" EventName="Initialized" />
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>
using System;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DocumentManagerSerialization.Properties;
using System.Collections.Generic;
using DocumentManagerSerialization.Common;

//...
[POCOViewModel]
public class MainViewModel : ISupportLogicalLayout {
    public ILayoutSerializationService LayoutSerializationService { get { return this.GetService<ILayoutSerializationService>(); } }

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

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

View Example

Inheritance

Object
DispatcherObject
DependencyObject
Freezable
Animatable
DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase
DevExpress.Mvvm.UI.Interactivity.Behavior
DevExpress.Mvvm.UI.Interactivity.Behavior<DependencyObject>
CurrentWindowSerializationBehavior
See Also