'WPF MVVM Datagrid Save User-Defined Column Order

Can anyone point me to a solution where i can save the column sort order of a wpf-datagrid in a mvvm scenario? Most solutions i have found so far are mostly utilizing some sort of code-behind logic.



Solution 1:[1]

One way to achieve your requirements would be to extend the DataGrid class and export the column order to a text or XML file that can be reloaded the next time that you use the control.

You can find a full tutorial with code examples on how to do that in the DataGridView that Saves Column Order, Width and Visibility to user.config page on CodeProject.

Solution 2:[2]

Just came Across this question and Solved it by using Behaviours and Saved the Results to Properties.Settings.Default and thought i share the solution.

The Behaviour:

public class SaveDatagridColumnOrderBehaviour : Behavior<DataGrid>
{
    // An Dependency Property to pass the Name of the Setting, where the result is saved.
    public static readonly DependencyProperty SettingProperty = DependencyProperty.RegisterAttached(nameof(SettingName), typeof(string), typeof(SaveDatagridColumnOrderBehaviour));
    public string SettingName
    {
        get
        {
            return (string)GetValue(SettingProperty);
        }
        set
        {
            SetValue(SettingProperty, value);
        }
    }

    //The actual Behaviour
    protected override void OnAttached()
    {
        // Load the indexes to Datagrid on Loaded
        AssociatedObject.Loaded += (sender, e) =>
        {
            var grid = (sender as DataGrid);
            // Get the previously saved Result from my Properties.Settings. Make Sure you pass the correct setting name
            var displayIndexes = Properties.Settings.Default[SettingName].ToString()?.Split(',');
            if (displayIndexes.Count() == grid.Columns.Count())
            {
                for (int i = 0; i < grid.Columns.Count(); i++)
                {
                    if (int.TryParse(displayIndexes[i], out int index))
                        grid.Columns[i].DisplayIndex = index;
                }
            }

        };

        //Save the Values when a column i reordered.
        AssociatedObject.ColumnReordered += (sender, e) =>
        {
            var grid = (sender as DataGrid);
            List<int> displayIndexes = new List<int>();
            for (int i = 0; i < grid.Columns.Count(); i++)
            {
                displayIndexes.Add(grid.Columns[i].DisplayIndex);
            }

            //Save the Result to my Properties.Settings.
            Properties.Settings.Default[SettingName] = string.Join(",", displayIndexes);
            Properties.Settings.Default.Save();
        };
    }
}

To use the behaviour make sure you have the needed Namespaced in your xaml file.

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:b="clr-namespace:YourProjectName.Behaviours"

To attach the behaviour to your Datagrid

        <DataGrid>
            <i:Interaction.Behaviors>
                <b:SaveDatagridColumnOrderBehaviour
                    SettingName="NotesOverViewGridDisplayIndexOrder" />
            </i:Interaction.Behaviors>
        </DataGrid>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sheridan
Solution 2