LIDOR SYSTEMS

Advanced User Interface Controls and Components

XML Serialization of Custom Color Schemes in TreeListView .NET

Created: 01 September 2013

The appearance of TreeListView control is customized by using many different color and format styles. By using these styles we are free to change the look of any control part. And although there are some built-in themes which can further be customized, in most cases we also want to save our changes for later use.

One way to achieve this is by using serialization techniques. However, we want to serialize only the appearance of the control, and not its data. This can be done by using built-in methods for serialization, which allows you to choose whether you want to serialize the content in whole or only data or appearance separately.

TreeListView XML Serialization of Custom Color Schemes
Download Sample - TreeListView XML Serialization of Custom Color Schemes

In this article we will show you how to create your own color schemes and write them to XML files and later when required quickly change the appearance of the control by simple loading these files. For demonstration purpose we have created three XML files, which contain red, green and blue color schemes. You can find these files in sample project

At first we need to change some properties of color styles for columns, items and subitems. Then we can save those changes to a custom XML file. We need to make sure that we only save changes to the styles to a XML file, so when later we read those files only the appearance of the control will change. This is done by setting the SerializationType to Style:

TreeListViewSerializer serializer = new TreeListViewSerializer();

serializer.SaveToXml(this.treeListView1, saveDlg.FileName, SerializationType.Style);

Dim serializer As New TreeListViewSerializer()

serializer.SaveToXml(Me.treeListView1, saveDlg.FileName, SerializationType.Style)

Related: Load/Save Partial Data to XML files

Later when we want to change the control appearance, we can use the default method. This method will read and apply in whole the content of XML file to the target TreeListView control. However, because previously the files were created by using the Style value for SerializationType, only changes to the appearance of TreeListView will be applied, and the data will remain the same.

// Suspend the control layout from updating to increase performance

this.treeListView1.SuspendUpdate();

 

TreeListViewSerializer serializer = new TreeListViewSerializer();

 

OpenFileDialog openDlg = new OpenFileDialog();

openDlg.Filter = "XML files (*.xml)|*.xml";

openDlg.InitialDirectory = Application.ExecutablePath;

 

if (openDlg.ShowDialog() == DialogResult.OK)

serializer.LoadFromXml(this.treeListView1, openDlg.FileName);

 

// Resume the layout calculations and update the control

this.treeListView1.ResumeUpdate();

' Suspend the control layout from updating to increase performance

Me.treeListView1.SuspendUpdate()

 

Dim serializer As New TreeListViewSerializer()

 

Dim openDlg As New OpenFileDialog()

openDlg.Filter = "XML files (*.xml)|*.xml"

openDlg.InitialDirectory = Application.ExecutablePath

 

If openDlg.ShowDialog() = DialogResult.OK Then

serializer.LoadFromXml(Me.treeListView1, openDlg.FileName)

End If

 

' Resume the layout calculations and update the control

Me.treeListView1.ResumeUpdate()

A sample project in C# and VB showing how to read and write custom color schemes to XML files is available for download from here: TreeListView Color Schemes Serialization

Did you Like this Article?


Enter your e-mail address below and you will receive latest articles as well as news on upcoming events and special offers.