Syncfusion AI Assistant

How can I help you?

Serialization and Deserialization in WPF TreeGrid

18 Mar 202616 minutes to read

SfTreeGrid allows you to serialize and deserialize the SfTreeGrid settings using DataContractSerializer.

Serialization

You can serialize the SfTreeGrid by using SfTreeGrid.Serialize method which exports the current TreeGrid control properties to an XML file.

using (var file = File.Create("TreeGrid.xml"))
{           
    treeGrid.Serialize(file);
}    

Serialize as Stream

You can store the SfTreeGrid settings as Stream using Serialize method by passing the stream.

FileStream stream = new FileStream("TreeGrid", FileMode.Create);
this.treeGrid.Serialize(stream);

Serialization options

SfTreeGrid serialization operation can be customized by passing TreeGridSerializationOptions instance as an argument to Serialize method.

Serialize sorting

By default, SfTreeGrid allows you to serialize the sorting operation. You can disable the sorting serialization by setting the TreeGridSerializationOptions.SerializeSorting to false.

using (var file = File.Create("TreeGrid.xml"))
{
    TreeGridSerializationOptions options = new TreeGridSerializationOptions();
    options.SerializeSorting = false;
    treeGrid.Serialize(file, options);
}

Serialize filtering

By default, SfTreeGrid allows you to serialize the filtering operation. You can disable the filtering serialization by setting the TreeGridSerializationOptions.SerializeFiltering to false.

using (var file = File.Create("TreeGrid.xml"))
{
    TreeGridSerializationOptions options = new TreeGridSerializationOptions();
    options.SerializeFiltering = false;
    treeGrid.Serialize(file, options);
}

Serialize columns

By default, SfTreeGrid allows you to serialize the columns manipulation operation. You can disable the columns serialization by setting the TreeGridSerializationOptions.SerializeColumns to false.

using (var file = File.Create("TreeGrid.xml"))
{
    TreeGridSerializationOptions options = new TreeGridSerializationOptions();
    options.SerializeColumns = false;
    treeGrid.Serialize(file, options);
}

Serialize stacked headers

By default, SfTreeGrid allows you to serialize the stack headers operation. You can disable the stack headers serialization by setting the TreeGridSerializationOptions.SerializeStackedHeaders to false.

using (var file = File.Create("TreeGrid.xml"))
{
    TreeGridSerializationOptions options = new TreeGridSerializationOptions();
    options.SerializeStackHeaders = false;
    treeGrid.Serialize(file, options);
}

Deserialization

You can deserialize the SfTreeGrid setting by using SfTreeGrid.Deserialize method which reconstructs the SfTreeGrid based on the setting in the stored XML file.

using (var file = File.Open("TreeGrid.xml", FileMode.Open))
{
    treeGrid.Deserialize(file);
}    

Deserialize from Stream

You can deserialize the SfTreeGrid settings from Stream using Deserialize method.

FileStream fileStream = new FileStream("TreeGrid", FileMode.Open);
this.treeGrid.Deserialize(fileStream);

Deserialization options

Deserialization operation can be customized by passing TreeGridDeserializationOptions instance as an argument to Deserialize method.

Deserialize sorting

By default, SfTreeGrid allows you to deserialize the sorting operation. You can disable the sorting deserialization by setting the TreeGridDeserializationOptions.DeserializeSorting to false.

using (var file = File.Open("TreeGrid.xml", FileMode.Open))
{
    TreeGridDeserializationOptions options = new TreeGridDeserializationOptions();
    options.DeserializeSorting = false ;
    treeGrid.Deserialize(file, options);
}

Deserialize filtering

By default, SfTreeGrid allows you to deserialize the filtering. you can disable the filtering deserialization by setting TreeGridDeserializationOptions.DeserializeFiltering to false.

using (var file = File.Open("TreeGrid.xml", FileMode.Open))
{
    TreeGridDeserializationOptions options = new TreeGridDeserializationOptions();
    options.DeserializeFiltering = false ;
    treeGrid.Deserialize(file, options);
}

Deserialize columns

By default, SfTreeGrid allows you to deserialize the columns manipulation operations. You can disable the columns deserialization by setting the TreeGridDeserializationOptions.DeserializeColumns to false.

using (var file = File.Open("TreeGrid.xml", FileMode.Open))
{
    TreeGridDeserializationOptions options = new TreeGridDeserializationOptions();
    options.DeserializeColumns = false ;
    treeGrid.Deserialize(file, options);
}

Deserialize stacked headers

By default, SfTreeGrid allows you to deserialize the stack headers. You can disable the stacked headers deserialization by setting the TreeGridDeserializationOptions.DeserializeStackedHeaders to false.

using (var file = File.Open("TreeGrid.xml", FileMode.Open))
{
    TreeGridDeserializationOptions options = new TreeGridDeserializationOptions();
    options.DeserializeStackedHeaders = false ;
    treeGrid.Deserialize(file, options);
}

Customizing Serialization and Deserialization Operations

SfTreeGrid allows you to customize the serialization and deserialization operations by deriving TreeGridSerializationController class and override the necessary virtual methods.

Serialize custom column

By default, the unknown(custom) column types are serialized as TreeGridTextColumn type. If you want to serialize the custom column, you have to add custom column type into predefined types.

In the below code snippet, TreeGridDatePickerColumn is created . For more information about creating custom column refer here.

public class TreeGridDatePickerColumn : TreeGridColumn
{        
    public TreeGridDatePickerColumn()
    {
        SetCellType("DatePickerRenderer");
    }

    public static readonly DependencyProperty DateMappingNameProperty = DependencyProperty.Register("DateMappingName",
typeof(string), typeof(TreeGridDatePickerColumn));

    public string DateMappingName
    {
        get { return (string)GetValue(DateMappingNameProperty); }
        set { SetValue(DateMappingNameProperty, value); }
    }

    protected override Freezable CreateInstanceCore()
    {
        return new TreeGridDatePickerColumn();
    }

    protected override void SetDisplayBindingConverter()
    {
        (this.DisplayBinding as Binding).Converter = new CustomConverter();      
    }
}

In the below code snippet, the TreeGridDatePickerColumn is defined in SfTreeGrid.

<syncfusion:SfTreeGrid Name="treeGrid"
              ChildPropertyName="ReportsTo"
              ItemsSource="{Binding Employees}"
              ParentPropertyName="ID"
              AutoGenerateColumns="False"
              AllowEditing="True">

    <syncfusion:SfTreeGrid.Columns>
        <local:TreeGridDatePickerColumn HeaderText="DOJ" MappingName="DOJ" DateMappingName="JoiningDate"/>
    </syncfusion:SfTreeGrid.Columns>

</syncfusion:SfTreeGrid>

To serialize the above TreeGridDatePickerColumn, follow the below steps.

  1. Create a class derived from SerializableTreeGridColumn and define the custom column properties in SerializableCustomTreeGridColumn class.
[DataContract(Name="SerializableCustomTreeGridColumn")]
public class SerializableCustomTreeGridColumn : SerializableTreeGridColumn
{
    [DataMember]
    public string DateMappingName { get; set; }
}
  1. Create a new class named as TreeGridSerializationControllerExt by overriding TreeGridSerializationController class.
treeGrid.SerializationController = new TreeGridSerializationControllerExt(treeGrid);

public class TreeGridSerializationControllerExt : TreeGridSerializationController
{
 
    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
        : base(treeGrid)
    {
    }
}
  1. You can get the custom column property settings for serialization by overriding the GetSerializableTreeGridColumn virtual method.
public class TreeGridSerializationControllerExt : TreeGridSerializationController
{

    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
        : base(treeGrid)
    {
    }

    protected override SerializableTreeGridColumn GetSerializableTreeGridColumn(TreeGridColumn column)
    {

        if (column.MappingName == "DOJ")
        {
            return new SerializableCustomTreeGridColumn();
        }
        return base.GetSerializableTreeGridColumn(column);
    }
}
  1. Store the custom column property settings during serialization by overriding the StoreTreeGridColumnProperties virtual method.
public class TreeGridSerializationControllerExt : TreeGridSerializationController
{

    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
            : base(treeGrid)
    {
    }

    protected override void StoreTreeGridColumnProperties(TreeGridColumn column, SerializableTreeGridColumn serializableColumn)
    {
        base.StoreTreeGridColumnProperties(column, serializableColumn);

        if (column is TreeGridDatePickerColumn)
            (serializableColumn as SerializableCustomTreeGridColumn).DateMappingName = (column as TreeGridDatePickerColumn).DateMappingName;
    }
}
  1. Add the custom column into known column types by overriding the KnownTypes virtual method.
public class TreeGridSerializationControllerExt : TreeGridSerializationController
{
    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
            : base(treeGrid)
    {
    }

    public override Type[] KnownTypes()
    {
        var types = base.KnownTypes();
        var list = types.Cast<Type>().ToList();
        list.Add(typeof(SerializableCustomTreeGridColumn));
        return list.ToArray();
    }
}
  1. During deserialization, you can get the custom column settings from SerializableTreeGridColumn by overriding GetTreeGridColumn virtual method.
public class TreeGridSerializationControllerExt : TreeGridSerializationController
{
    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
            : base(treeGrid)
    {
    }

    protected override TreeGridColumn GetTreeGridColumn(SerializableTreeGridColumn serializableColumn)
    {

        if (serializableColumn is SerializableCustomTreeGridColumn)
            return new TreeGridDatePickerColumn();
            return base.GetTreeGridColumn(serializableColumn);
    }
}
  1. Now restore the custom column settings from SerializableTreeGridColumn by overriding the RestoreColumnProperties virtual method.
public class TreeGridSerializationControllerExt : TreeGridSerializationController
{

    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
        : base(treeGrid)
    {
    }

    protected override void RestoreColumnProperties(SerializableTreeGridColumn serializableColumn, TreeGridColumn column)
    {
        base.RestoreColumnProperties(serializableColumn, column);
        
        if (column is TreeGridDatePickerColumn)
            (column as TreeGridDatePickerColumn).DateMappingName = (serializableColumn as SerializableCustomTreeGridColumn).DateMappingName;
    }

}

Serializing template column content

By default, you cannot serialize the template content in SfTreeGrid. This is the default behavior during Serialization and Deserialization operation.

<Application.Resources>
    <DataTemplate x:Key="cellTemplate">
        <Button Content="{Binding Value}" />
    </DataTemplate>
</Application.Resources>

<syncfusion:SfTreeGrid Name="treeGrid"
              ChildPropertyName="ReportsTo"
              ItemsSource="{Binding Employees}"
              ParentPropertyName="ID"
              AutoGenerateColumns="False">

    <syncfusion:SfTreeGrid.Columns>

        <syncfusion:TreeGridTemplateColumn CellTemplate="{StaticResource cellTemplate}"
                                       HeaderText="Salary"
                                       MappingName="Salary"
                                       SetCellBoundValue="True" />        
    </syncfusion:SfTreeGrid.Columns>

</syncfusion:SfTreeGrid>

If you want to serialize and deserialize the template content, you have to reconstruct the same template during deserialization in RestoreColumnProperties method.

this.treeGrid.SerializationController = new TreeGridSerializationControllerExt(this.treeGrid);

public class TreeGridSerializationControllerExt : TreeGridSerializationController
{

    public TreeGridSerializationControllerExt(SfTreeGrid treeGrid)
        : base(treeGrid)
    {

    }
        
    protected override void RestoreColumnProperties(SerializableTreeGridColumn serializableColumn, TreeGridColumn column)
    {
        base.RestoreColumnProperties(serializableColumn, column);

        if (column is TreeGridTemplateColumn)
        {

            if (column.MappingName == "Salary")
            {
                column.CellTemplate = App.Current.Resources["cellTemplate"] as DataTemplate;
            }
        }
    }
}