Skip to main content

Save and Load Settings

All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.

Persisting and loading settings using an XmlSerializer

The following example demonstrates how to persist and load settings using an XML serializer.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}" />
</Grid.Resources>
<DockPanel>
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Top">
<Button Content="Save Settings"
Click="SaveSettings" />
<Button Content="Load Settings"
Click="LoadSettings" />
</StackPanel>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}">
</xcdg:DataGridControl>
</DockPanel>
</Grid>

Persisting and loading settings through the application settings

The following example demonstrates how to persist and load settings using the application settings.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}" />
</Grid.Resources>
<DockPanel>
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Top">
<Button Content="Save Settings"
Click="SaveSettings" />
<Button Content="Load Settings"
Click="LoadSettings" />
</StackPanel>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}">
</xcdg:DataGridControl>
</DockPanel>
</Grid>

In addition to persisting and load the grid-specific settings to and from the resources, the application settings themselves must also be persisted using the default setting's Reload and Save methods, which are usually called in the application's OnStartup and OnExit overrides, respectively.

  protected override void OnStartup( StartupEventArgs e )
{
Xceed.Wpf.DataGrid.Licenser.LicenseKey = "license_key";
base.OnStartup( e );
Settings.Default.Reload();
}
protected override void OnExit( ExitEventArgs e )
{
Settings.Default.Save();
base.OnExit( e );
}