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.
- XAML
- C#
- VB.NET
<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>
private void SaveSettings( object sender, RoutedEventArgs e )
{
SettingsRepository settings = new SettingsRepository();
this.OrdersGrid.SaveUserSettings( settings, UserSettings.All );
XmlSerializer serializer = new XmlSerializer( typeof( SettingsRepository ) );
using( FileStream stream = new FileStream( "d:\\settings.xml", FileMode.CreateNew ) )
{
serializer.Serialize( stream, settings );
}
}
private void LoadSettings( object sender, RoutedEventArgs e )
{
SettingsRepository settings;
using( FileStream stream = new FileStream( "d:\\settings.xml", FileMode.Open ) )
{
XmlSerializer serializer = new XmlSerializer( typeof( SettingsRepository ) );
settings = serializer.Deserialize( stream ) as SettingsRepository;
}
this.OrdersGrid.LoadUserSettings( settings, UserSettings.All );
}
Private Sub SaveSettings( ByVal sender As Object, ByVal e As RoutedEventArgs )
Dim settings As New SettingsRepository()
Me.OrdersGrid.SaveUserSettings( settings, UserSettings.All )
Dim serializer As New XmlSerializer( GetType( SettingsRepository ) )
Using stream As New FileStream( "d:\settings.xml", FileMode.CreateNew )
serializer.Serialize( stream, settings )
End Using
End Sub
Private Sub LoadSettings( ByVal sender As Object, ByVal e As RoutedEventArgs )
Dim settings As SettingsRespository
Using stream As New FileStream( "d:\settings.xml", FileMode.Open )
Dim serializer As New XmlSerializer( GetType( SettingsRepository ) )
settings = TryCast( serializer.Deserialize( stream ), SettingsRepository )
End Using
Me.OrdersGrid.LoadUserSettings( settings, UserSettings.All )
End Sub
Persisting and loading settings through the application settings
The following example demonstrates how to persist and load settings using the application settings.
- XAML
- C#
- VB.NET
<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>
private void SaveSettings( object sender, RoutedEventArgs e )
{
if( Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings == null )
{
Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings = new SettingsRepository();
}
this.OrdersGrid.SaveUserSettings( Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings, UserSettings.All );
}
private void LoadSettings( object sender, RoutedEventArgs e )
{
if( Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings == null )
return;
this.OrdersGrid.LoadUserSettings( Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings, UserSettings.All );
}
Private Sub SaveSettings( ByVal sender As Object, ByVal e As RoutedEventArgs )
If Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings Is Nothing Then
Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings = New SettingsRepository()
End If
Me.OrdersGrid.SaveUserSettings( Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings, UserSettings.All )
End Sub
Private Sub LoadSettings( ByVal sender As Object, ByVal e As RoutedEventArgs )
If Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings Is Nothing Then
Return
End If
Me.OrdersGrid.LoadUserSettings( Xceed.Wpf.Documentation.Properties.Settings.Default.GridSettings, UserSettings.All )
End Sub
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.
- C#
- VB.NET
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 );
}
Protected Overrides Sub OnStartup( ByVal e As StartupEventArgs )
Xceed.Wpf.DataGrid.Licenser.LicenseKey = "license_key"
MyBase.OnStartup( e )
Settings.Default.Reload()
End Sub
Protected Override Sub OnExit( ByVal e As ExitEventArgs )
Settings.Default.Save()
MyBase.OnExit( e )
End Sub