WPF ListView: maintaining column ordering

For a WPF project, you need to keep the width and order of the columns of the ListView, because these are things that the user can change. I think this is not a problem getting the current width, but the current position seems a bit complicated.

There was something like index and displayIndex in WinForms, but I do not see it in WPF. How it's done?

BTW: Serializing the entire control is not an option.

Edit:

I found several samples using the listView.columns property. But I do not have such a property in my View list

My XAML code is as follows:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn>
            ....
+5
source share
2 answers

I managed to do this using the Move (...) method of the GridView Columns collection

- , :

((GridView)myListView.View).Columns.Move(originalIndex, newIndex);

: XAML, .xaml.cs

+4

, gridView.Columns. gridView.CollectionChanged, , . WPF Listview: ?

Behavior . , DataContext. System.Windows.Interactivity.

DataContext ObservableCollection of ColumnInfo, :

public class ColumnInfo
    {
        public string HeaderName { get; set; }
        public int Width { get; set; }
        public int Index { get; set; }
    }

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

ListView

<ListView ItemsSource="{Binding SomeCollection}">
    <ListView.View>
        <GridView>
            <i:Interaction.Behaviors>
                <b:GridViewColumnBehavior Columns="{Binding Columns}" />
            </i:Interaction.Behaviors>
        </GridView>
    </ListView.View>
</ListView>

, ( )

public class GridViewColumnBehavior : Behavior<GridView>
{
    public ObservableCollection<ColumnInfo> Columns
    {
        get { return (ObservableCollection<ColumnInfo>)GetValue(ColumnsProperty); }
        set { SetValue(ColumnsProperty, value); }
    }

    public static readonly DependencyProperty ColumnsProperty =
        DependencyProperty.Register("Columns", typeof(ObservableCollection<ColumnInfo>), typeof(GridViewColumnBehavior), new PropertyMetadata(null, new PropertyChangedCallback(Columns_Changed)));

    private static void Columns_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = d as GridViewColumnBehavior;
        if (b == null) return;

        b.SetupColumns(e.NewValue as ObservableCollection<Column>);
    }

    public void SetupColumns(ObservableCollection<Column> oldColumns)
    {
        if(oldColumns != null)
        {
            oldColumns.CollectionChanged -= Columns_CollectionChanged;
        }

        if ((Columns?.Count ?? 0) == 0) return;

        AssociatedObject.Columns.Clear();

        foreach (var column in Columns.OrderBy(c => c.Index))
        {
            AddColumn(column);
        }

        Columns.CollectionChanged += Columns_CollectionChanged;
    }

    private void Columns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        var lookup = AssociatedObject.Columns.Select((c, i) => new { Index = i, Element = c.Header.ToString() }).ToLookup(ci => ci.Element, ci => ci.Index);
        foreach (var c in Columns)
        {
            // store the index in the Model (ColumnInfo)
            c.Index = lookup[c.HeaderName].FirstOrDefault();
        }
    }
}

!

0

All Articles