How to Save and Restore the Dashboard State

This example demonstrates how to manage the dashboard state to save and restore selected master filters values, current drill-down levels and other selections such as the selected Treemap layer.

When the main window closes, the GetDashboardState() method obtains a dashboard state object. It is serialized to XML and added to the XElement object stored in the Dashboard.UserData property. Subsequently, the dashboard with the dashboard state data is saved to a file.

When the application starts, the DashboardControl loads the dashboard and the DashboardState object is deserialized in the DashboardLoaded event handler.

The dashboard state is restored using the SetDashboardState(DashboardState) in the SetInitialDashboardState event handler.

Note

The complete sample project How to Set the Initial Dashboard State is available in the DevExpress Examples repository.

using DevExpress.DashboardCommon;
using DevExpress.DashboardWpf;
using System;
using System.Windows;
using System.Xml.Linq;

namespace WpfDashboard_DashboardState
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DashboardState dState = new DashboardState();
        public MainWindow()
        {
            InitializeComponent();
        }
        private void dashboardControl_DashboardLoaded(object sender, DashboardLoadedEventArgs e)
        {
            XElement data = e.Dashboard.UserData;
            if (data != null)
            {
                if (data.Element("DashboardState") != null)
                {
                    XDocument dStateDocument = XDocument.Parse(data.Element("DashboardState").Value);
                    dState.LoadFromXml(XDocument.Parse(data.Element("DashboardState").Value));
                }
            }
        }
        private void dashboardControl_SetInitialDashboardState(object sender, SetInitialDashboardStateWpfEventArgs e)
        {
            e.InitialState = dState;
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            dState = dashboardControl.GetDashboardState();
            XElement userData = new XElement("Root", 
                new XElement("DateModified", DateTime.Now), 
                new XElement("DashboardState", dState.SaveToXml().ToString(SaveOptions.DisableFormatting)));
            dashboardControl.Dashboard.UserData = userData;
            dashboardControl.Dashboard.SaveToXml("SampleDashboardWithState.xml");
        }
    }
}