Xaml Serialization to the rescue.
The following sample code will persist the current Listview state to disk and restore it the next time the control is loaded.
Code behind
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void lvInit(object sender, EventArgs e)
{
GridView gv = lvTest.View as GridView;
gv.Columns.CollectionChanged += (s, gvE) =>
{
if (gvE.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move)
{
SaveState();
}
};
}
private void SaveState()
{
string lvXaml = XamlWriter.Save(lvTest);
using (var s = new StreamWriter(@"c:\temp\lv.xaml"))
{
s.Write(lvXaml);
}
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
ListView li = null;
if (File.Exists(@"c:\temp\lv.xaml"))
bdr.Child = (ListView)XamlReader.Load(File.OpenRead(@"c:\temp\lv.xaml"));
}
}
}
About Brian Keating
Professional Software Developer, .NET / C++ / Java View all posts by
Brian Keating →