Xceed DataGrid for WPF v7.3 Documentation
Welcome to Xceed DataGrid, Editors, and 3D Views for WPF v7.3 / Xceed DataGrid for WPF / Code Snippets / Save and Load Settings

In This Topic
    Save and Load Settings
    In This Topic

    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
    Copy Code
    <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>

    The following code provides the implementation required to persist and load settings using the XmlSerializer class. 

    VB.NET
    Copy Code
    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
    C#
    Copy Code
    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 );
    }

    Persisting and loading settings through the application settings

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

    XAML
    Copy Code
    <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>

    The following code provides the implementation required to persist and load settings using the application settings.

    VB.NET
    Copy Code
    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
    C#
    Copy Code
    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 );
    }

    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.

    VB.NET
    Copy Code
    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
    C#
    Copy Code
    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 );
    }