Вот большое начало. Осталось только пройтись по элементам ListView в GridView и установить в строке элемент foreach index tab (//! iterate ListView rows
). Некоторая логика может быть испорчена, поскольку код был поспешен ..
XAML
<GridView
AllowsColumnReorder="True"
Controls:GridViewExtensions.DoTabIndexing="True">...
C #
/// <summary>Provides members helpful to <see cref="GridView"/>.</summary>
public static class GridViewExtensions
{
#region DoTabIndexing
[Category("Common")]
[AttachedPropertyBrowsableForType(typeof(GridView))]
public static bool GetDoTabIndexing(GridView gridView)
{
return (bool)gridView.GetValue(DoTabIndexingProperty);
}
public static void SetDoTabIndexing(GridView gridView, bool value)
{
gridView.SetValue(DoTabIndexingProperty, value);
}
public static readonly DependencyProperty DoTabIndexingProperty =
DependencyProperty.RegisterAttached(
"DoTabIndexing",
typeof(bool), // type
typeof(GridViewExtensions), // container/holder/control
new PropertyMetadata(default(bool), OnDoTabIndexingChanged)
);
private static void OnDoTabIndexingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var gridView = (GridView)d;
if (gridView.AllowsColumnReorder == false) { return; }
var newValue = (bool)e.NewValue;
_indexWatch = new ColumnIndexWatch(gridView);
}
static ColumnIndexWatch _indexWatch;
#endregion DoTabIndexing
/// <summary>Watches for changes in a <see cref="GridView"/>'s columns.</summary>
class ColumnIndexWatch
{
readonly GridView _gridView;
public ColumnIndexWatch(GridView gridView)
{
_gridView = gridView;
gridView.Columns.CollectionChanged += OnItemsPopulated;
}
void OnItemsPopulated(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action != NotifyCollectionChangedAction.Add)
{
_gridView.Columns.CollectionChanged -= OnItemsPopulated;
_gridView.Columns.CollectionChanged += OnItemMoved;
trax = new ColumnIndexCollection(_gridView.Columns);
OnItemMoved(sender, e);
}
}
ColumnIndexCollection trax;
void OnItemMoved(object sender, NotifyCollectionChangedEventArgs e)
{
var movedColumn = e.NewItems[0] as GridViewColumn;
if (movedColumn == null) { return; }
trax.ApplyNewIndex(movedColumn, e.NewStartingIndex);
}
/// <summary>Represents a collection of <see cref="ColumnIndex"/></summary>
class ColumnIndexCollection : Collection<ColumnIndex>
{
public ColumnIndexCollection(IEnumerable<GridViewColumn> columns)
: base(Create(columns)) { }
static IList<ColumnIndex> Create(IEnumerable<GridViewColumn> columns)
{
return columns.Select((t, i) => new ColumnIndex { GridViewColumn = t, Index = i }).ToList();
}
public void ApplyNewIndex(GridViewColumn column, int newIndex)
{
var movedByUser = Items.First(col => col.GridViewColumn == column);
var placeTaken = Items.First(col => col.Index == newIndex);
placeTaken.Index = movedByUser.Index;
movedByUser.Index = newIndex;
movedByUser.Update();
placeTaken.Update();
//! iterate ListView rows
}
}
/// <summary>Represents a <see cref="System.Windows.Controls.GridViewColumn"/> and its index.</summary>
class ColumnIndex
{
public GridViewColumn GridViewColumn { get; set; }
public int Index { get; set; }
public void Update()
{
KeyboardNavigation.SetTabIndex(GridViewColumn, Index);
}
public override string ToString()
{
return string.Format("{0} : {1}", Index, GridViewColumn);
}
}
}
}