Example #1
0
 /// Gets the sorting direction of the header clicked.
 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);
     }
 }
Example #2
0
        /// Grids the view column header clicked handler.
        public static void GridViewColumnHeaderClickedHandler(Object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.ListView view = sender as System.Windows.Controls.ListView;
            if (view == null)
            {
                return;
            }

            if (_listViewDefinitions == null)
            {
                return;
            }

            ListViewSortItem listViewSortItem = (_listViewDefinitions.ContainsKey(view.Name)) ? _listViewDefinitions[view.Name] : null;

            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);

            // get header name
            String header = headerClicked.Column.Header as string;

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

            // sort listview
            if (listViewSortItem.Comparer != null)
            {
                listViewSortItem.Comparer.SortBy        = header;
                listViewSortItem.Comparer.SortDirection = sortDirection;
                collectionView.CustomSort = listViewSortItem.Comparer;
                view.Items.Refresh();
            }
            else
            {
                view.Items.SortDescriptions.Clear();
                view.Items.SortDescriptions.Add(new SortDescription(headerClicked.Column.Header.ToString(), sortDirection));
                view.Items.Refresh();
            }

            // change datatemplate of previous and current column header
            headerClicked.Column.HeaderTemplate = GetHeaderColumnsDataTemplate(view, listViewSortItem, sortDirection);

            // Set current sort values as last sort values
            listViewSortItem.LastColumnHeaderClicked = headerClicked;
            listViewSortItem.LastSortDirection       = sortDirection;
        }
Example #3
0
        /// Gets the header columns data template.
        private static DataTemplate GetHeaderColumnsDataTemplate(System.Windows.Controls.ListView view, ListViewSortItem listViewSortItem, ListSortDirection sortDirection)
        {
            // remove mark from previous sort column
            if (listViewSortItem.LastColumnHeaderClicked != null)
            {
                listViewSortItem.LastColumnHeaderClicked.Column.HeaderTemplate = view.TryFindResource("ListViewHeaderTemplateNoSorting") as DataTemplate;
            }

            // set correct mark to current column
            switch (sortDirection)
            {
            case ListSortDirection.Ascending:
                return(view.TryFindResource("ListViewHeaderTemplateAscendingSorting") as DataTemplate);

            case ListSortDirection.Descending:
                return(view.TryFindResource("ListViewHeaderTemplateDescendingSorting") as DataTemplate);

            default:
                return(null);
            }
        }