private void HandleVisibleColumnsChanged() { if (AssociatedObject == null) { return; } // Not using LINQ to improve UI responsiveness... // ReSharper disable once ForCanBeConvertedToForeach foreach (var columnName in _columnsName) { _columns[columnName].Visibility = Visibility.Hidden; } var visibleColumns = VisibleColumns.ToArray(); for (var i = 0; i < visibleColumns.Length; i++) { var column = _columns[visibleColumns[i]]; var oldIndex = AssociatedObject.Columns.IndexOf(column); var newIndex = i; column.Visibility = Visibility.Visible; if (oldIndex != newIndex) { AssociatedObject.Columns.RemoveAt(oldIndex); AssociatedObject.Columns.Insert(newIndex, column); } } }