private static void OnListViewHeaderClick(object sender, RoutedEventArgs e) { ListView listView = e.Source as ListView; GridViewColumnHeader header = e.OriginalSource as GridViewColumnHeader; SortInfo sortInfo = listView.GetValue(SortInfoProperty.DependencyProperty) as SortInfo; if (sortInfo != null) { AdornerLayer.GetAdornerLayer(sortInfo.LastSortColumn).Remove(sortInfo.CurrentAdorner); listView.Items.SortDescriptions.Clear(); } else { sortInfo = new SortInfo(); } if (sortInfo != null && header != null) { if (sortInfo.LastSortColumn == header) { (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection = (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; } else { sortInfo.CurrentAdorner = new UIElementAdorner(header, new ListSortDecorator()); } } else { return; } sortInfo.LastSortColumn = header; listView.SetValue(SortInfoProperty, sortInfo); AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner); SortDescription sortDescriptioin = new SortDescription() { Direction = (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection, PropertyName = header.Column.GetValue(SortFieldProperty) as string ?? header.Column.Header as string }; listView.Items.SortDescriptions.Add(sortDescriptioin); }
internal static void SetSortInfo(DependencyObject obj, SortInfo value) { obj.SetValue(SortInfoProperty.DependencyProperty, value); }