Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for persisting ListView columns layout in WPF

The goal is to let the user to hide/show/move/resize columns, to save this layout, and 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?

like image 294
Sergey Aldoukhov Avatar asked Dec 06 '25 17:12

Sergey Aldoukhov


1 Answers

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>
like image 173
Robert Macnee Avatar answered Dec 08 '25 08:12

Robert Macnee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!