How to: Save and Restore the Dashboard State
- 2 minutes to read
This example demonstrates how to manage the dashboard state to save and restore selected master filter 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.CustomProperties collection. 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 and restored using the GetDataFromString method in the in the SetInitialDashboardState event handler.
Note
Example on GitHub: How to Set the Initial Dashboard State
using DevExpress.DashboardCommon;
using System;
using System.Windows;
using System.Xml.Linq;
namespace WpfDashboard_DashboardState
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly string PropertyName = "DashboardState";
public MainWindow()
{
InitializeComponent();
}
DashboardState GetDataFromString(string customPropertyValue) {
DashboardState dState = new DashboardState();
if(!string.IsNullOrEmpty(customPropertyValue)) {
var xmlStateEl = XDocument.Parse(customPropertyValue);
dState.LoadFromXml(xmlStateEl);
}
return dState;
}
private void dashboardControl_DashboardLoaded(object sender, DevExpress.DashboardWpf.DashboardLoadedEventArgs e) {
}
private void dashboardControl_SetInitialDashboardState(object sender, DevExpress.DashboardWpf.SetInitialDashboardStateWpfEventArgs e)
{
var state = GetDataFromString(dashboardControl.Dashboard.CustomProperties.GetValue(PropertyName));
e.InitialState = state;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
var dState = dashboardControl.GetDashboardState();
var stateValue = dState.SaveToXml().ToString(SaveOptions.DisableFormatting);
dashboardControl.Dashboard.CustomProperties.SetValue("DashboardState", stateValue);
dashboardControl.Dashboard.SaveToXml("SampleDashboardWithState.xml");
}
//...
}
}