예제 #1
0
 private static ListSortDirection GetSortingDirection(GridViewColumnHeader headerClicked, ListViewSortItem listViewSortItem)
 {
     if (headerClicked != listViewSortItem.LastColumnHeaderClicked)
     {
         return(ListSortDirection.Ascending);
     }
     else
     {
         return((listViewSortItem.LastSortDirection == ListSortDirection.Ascending) ? ListSortDirection.Descending : ListSortDirection.Ascending);
     }
 }
예제 #2
0
        public static void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            ListView view = sender as ListView;

            if (view == null)
            {
                return;
            }

            ListViewSortItem listViewSortItem = _listViewDefinitions[view.Name];

            if (listViewSortItem == null)
            {
                return;
            }

            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

            if (headerClicked == null)
            {
                return;
            }

            ListCollectionView collectionView = CollectionViewSource.GetDefaultView(view.ItemsSource) as ListCollectionView;

            if (collectionView == null)
            {
                return;
            }

            ListSortDirection sortDirection = GetSortingDirection(headerClicked, listViewSortItem);

            string header = headerClicked.Tag as string;

            if (string.IsNullOrEmpty(header))
            {
                return;
            }

            if (listViewSortItem.Comparer != null)
            {
                view.Cursor = System.Windows.Input.Cursors.Wait;
                listViewSortItem.Comparer.SortBy        = header;
                listViewSortItem.Comparer.SortDirection = sortDirection;
                listViewSortItem.Comparer.DirectionDig  = ((sortDirection.Equals(ListSortDirection.Ascending)) ? -1 : 1);

                try { collectionView.CustomSort = listViewSortItem.Comparer; }
                catch { }

                view.Items.Refresh();
                view.Cursor = System.Windows.Input.Cursors.Arrow;
            }
            else
            {
                view.Items.SortDescriptions.Clear();
                view.Items.SortDescriptions.Add(new SortDescription(headerClicked.Column.Header.ToString(), sortDirection));
                view.Items.Refresh();
            }

            listViewSortItem.LastColumnHeaderClicked = headerClicked;
            listViewSortItem.LastSortDirection       = sortDirection;

            if ((listViewSortItem.Adorner != null) && (listViewSortItem.LastColumnHeaderClicked != null))
            {
                AdornerLayer.GetAdornerLayer(listViewSortItem.LastColumnHeaderClicked).Remove(listViewSortItem.Adorner);
            }

            switch (sortDirection)
            {
            case ListSortDirection.Ascending:
                listViewSortItem.Adorner = new SortAdorner(headerClicked, ListSortDirection.Ascending);
                AdornerLayer.GetAdornerLayer(headerClicked).Add(listViewSortItem.Adorner);
                break;

            case ListSortDirection.Descending:
                listViewSortItem.Adorner = new SortAdorner(headerClicked, ListSortDirection.Descending);
                AdornerLayer.GetAdornerLayer(headerClicked).Add(listViewSortItem.Adorner);
                break;
            }
        }