private void ReorderColumns() { _lockGridColumnsCollectionChanged = true; for (int c = 0; c < Grid.Columns.Count; c++) { var column = Grid.Columns.IndexOf(col => SortBehavior.GetFixedOrder(col) == c); if (column == -1) { break; } Grid.Columns.Move(column, c); } int i = 0; lock (ItemsSource) { foreach (string columnData in ItemsSource) { for (int j = i; j < Grid.Columns.Count; j++) { var column = Grid.Columns[j]; if (GetColumnId(column) == columnData) { Grid.Columns.Move(j, i + _staticColumnsCount); i++; break; } } } } _lockGridColumnsCollectionChanged = false; }
private void gridView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (!_lockGridColumnsCollectionChanged && e.Action == NotifyCollectionChangedAction.Move) { var oldIndex = e.OldStartingIndex - _staticColumnsCount; var newIndex = Math.Max(0, e.NewStartingIndex - _staticColumnsCount); if (oldIndex >= 0) { var collection = (ObservableCollection <string>)ItemsSource; collection.Move(oldIndex, newIndex); } else if (SortBehavior.GetFixedOrder(Grid.Columns[e.NewStartingIndex]) != -1) { ReorderColumns(); } } }