我想点击一列来排序。 Windows窗体上的一个简单任务....
按照WPF ListView sorting on column click
上的答案样本GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;
void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
ListSortDirection direction;
MessageBox.Show("Clic detected on column: " + headerClicked);
if (headerClicked != null)
{
if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
{
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Ascending;
}
else
{
if (_lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
}
string header = headerClicked.Column.Header as string;
Sort(header, direction);
_lastHeaderClicked = headerClicked;
_lastDirection = direction;
}
}
}
private void Sort(string sortBy, ListSortDirection direction)
{
MessageBox.Show("I'm inside of the Sort!");
ICollectionView dataView =
CollectionViewSource.GetDefaultView(listView.ItemsSource);
dataView.SortDescriptions.Clear();
SortDescription sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
我使用MessageBox来检查列是否被选中并且工作正常,即使列的内部名称也是如此,但listview保持不变。没有错误,没有排序。
有什么问题?
答案 0 :(得分:1)
这是经过试验和测试的“ListView sort on column header click”代码,您不仅可以在stackoverflow上重复使用,还可以在许多已扩展ListView的存储库中重复这些代码。我强烈怀疑您的问题与上述代码无关。