Version

Persistence Groups

Before You Begin

The Infragistics Persistence Framework™ can consolidate multiple controls’ settings in a group or groups. In this way it is possible to save your application’s state in a single data store or combine multiple controls’ settings within groups.

Note
Note:

You can save one Persistence Group and load it into another PersistenceGroup as long as the two groups have the same number and type of controls in the same order. Furthermore, you can save multiple controls’ settings in one group and then apply these settings to another group of controls.

What You Will Accomplish

This topic shows you how to create and use a PersistenceGroup object and combine two TextBox component settings into one group.

Follow these Steps

  1. Create a WPF Application project

  1. Add the following NuGet package to your application:

    • Infragistics.WPF.Persistence

    For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.

  1. Add the following namespaces in the UserControl tag

    In XAML:

    xmlns:ig="http://schemas.infragistics.com/xaml"
  1. Create a UserControl resource collection in the page and define a PersistenceGroup.

    • Add x:Key attribute with the name of the persistence group. The name uniquely identifies the group as a resource in the resource dictionary.

    • You can add as many groups as your application needs. The only requirement is that they should have unique x:Key attributes.

    • In this example you add only one persistence group to combine your control settings.

    In XAML:

    <UserControl.Resources>
        <ig:PersistenceGroup x:Key="CPF_Group" />
    </UserControl.Resources>
  1. Create a Grid container

    In XAML:

    <Grid x:Name="LayoutRoot">
       <Grid.RowDefinitions>
          <RowDefinition Height="*"/>
          <RowDefinition Height="Auto"/>
       </Grid.RowDefinitions>
       <!--Add more elements -->
  1. Add two TextBox controls in the page and use the PersistenceGroup attached property to specify that these controls belong to one persistence group defined as a static resource in the UserControl resource dictionary.

    In XAML:

    <StackPanel Grid.Row="0">
        <TextBox x:Name="txtFirstName" ig:PersistenceManager.PersistenceGroup="{StaticResource CPF_Group}" />
        <TextBox x:Name="txtLastName" ig:PersistenceManager.PersistenceGroup="{StaticResource CPF_Group}" />
    </StackPanel>
  1. Add two buttons for save and load.

    In XAML:

    <StackPanel Orientation="Horizontal" Grid.Row="1">
        <Button Content="Save" x:Name="BtnSave" Click="BtnSave_Click" Height="22" Width="100" />
        <Button Content="Load" x:Name="BtnLoad" Click="BtnLoad_Click" Height="22" Width="100" />
    </StackPanel>
  1. Add the following using/Import directives so that you don’t have to type out a member’s fully qualified name.

    In Visual Basic:

    Imports Infragistics.Persistence
    Imports System.IO

    In C#:

    using Infragistics.Persistence;
    using System.IO;
  1. Define several private members.

    In Visual Basic:

    ' Name of the PersistenceGroup defined in the XAML UserControl Resources
    Private Const pGroup As String = "CPF_Group"
    Private memoryStream As New MemoryStream()
    
    ' Creates PersistenceGroup object that will group controls' settings that will be saved and loaded at the same time
    Private persistenceGroup As New PersistenceGroup()

    In C#:

    // Name of the PersistenceGroup defined in the XAML UserControl Resources
    private const string pGroup = "CPF_Group";
    private MemoryStream memoryStream = new MemoryStream();
    
    // Creates PersistenceGroup object that will group controls' settings that will be saved and loaded at the same time
    private PersistenceGroup persistenceGroup = new PersistenceGroup();
  1. Add the following in the constructor of the page after the InitializeComponent() method.

    In Visual Basic:

    ' Gets already defined in XAML PersistenceGroup as a resource from ResourceDictionary
    persistenceGroup = TryCast(Me.Resources(pGroup), PersistenceGroup)

    In C#:

    // Gets already defined in XAML PersistenceGroup as a resource from ResourceDictionary
    persistenceGroup = this.Resources[pGroup] as PersistenceGroup;
  1. Handle the buttons’ Click events to save and load the persistence group.

    In Visual Basic:

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Saves PersistenceGroup in MemoryStream object
        memoryStream = PersistenceManager.Save(persistenceGroup)
    End Sub
    
    Private Sub BtnLoad_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
       memoryStream.Position = 0
       ' Loads PersistenceGroup from MemoryStream object
       PersistenceManager.Load(persistenceGroup, memoryStream)
    End Subs

    In C#:

    private void BtnSave_Click(object sender, RoutedEventArgs e)
    {
       // Saves PersistenceGroup in MemoryStream object
       memoryStream = PersistenceManager.Save(persistenceGroup);
    }
    
    private void BtnLoad_Click(object sender, RoutedEventArgs e)
    {
       memoryStream.Position = 0;
       // Loads PersistenceGroup from MemoryStream object
       PersistenceManager.Load(persistenceGroup, memoryStream);
    }
  1. Save and run your application. On the screen you have two TextBox controls. Type some values, and save their configuration, then type some new values into them. Then click the Load button. The stored changes are restored.

Related Topics