The goal is to let the user to hide/show/move/resize columns and save this layout to be able to restore it when the app re-starts. I'll tell you first how I do it. On GridView.Columns, I attach to CollectionChanged, as well as to each Column.With dependency property. When any of the events is fired, I save the order, visibility and widths of the Columns to a proprietary string which I save to an XML settings file. But it seems to me that this is a quite a bit of manual work - are there popular existing components or practices for saving the Columns layout?
+1
A:
Could you save the columns in XAML? After a quick test it seemed to save the relevant information. I started with this:
<GridView>
<GridView.Columns>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView.Columns>
</GridView>
And after resizing and moving a few, I called XamlWriter.Save
on GridView.Columns
and got this:
<GridViewColumnCollection xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<GridViewColumn Width="108.51" DisplayMemberBinding="{Binding Path=FirstName}">First Name</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=Age}">Age</GridViewColumn>
<GridViewColumn Width="83.8533333333333" DisplayMemberBinding="{Binding Path=LastName}">Last Name</GridViewColumn>
</GridViewColumnCollection>
Robert Macnee
2009-03-16 19:05:03
This is a good answer - my only reservation would be that when trying to restore, it would override your bindings and captions.
Sergey Aldoukhov
2009-03-17 00:26:59
You are correct. If you're only looking to store a few key properties I think your existing string method is fine. Saving the columns as XAML is kind of a nuclear option.
Robert Macnee
2009-03-17 22:32:53