예제 #1
0
        private static void Sort(string sortBy, ListSortDirection direction)
        {
            ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(lv.ItemsSource);

            if (view != null)
            {
                try
                {
                    ListViewCustomComparer sorter = (ListViewCustomComparer)ListViewSorter.GetCustomSorter(lv);
                    if (sorter != null)
                    {
                        // measuring timing of custom sort
                        int tick1 = Environment.TickCount;

                        sorter.AddSort(sortBy, direction);

                        view.CustomSort = sorter;
                        lv.Items.Refresh();

                        int tick2 = Environment.TickCount;

                        double elapsed1 = (tick2 - tick1) / 1000.0;

                        System.Windows.MessageBox.Show(elapsed1.ToString() + " seconds.");
                    }
                    else
                    {
                        //measuring timem of SortDescriptions sort
                        int tick3 = Environment.TickCount;

                        lv.Items.SortDescriptions.Clear();

                        SortDescription sd = new SortDescription(sortBy, direction);

                        lv.Items.SortDescriptions.Add(sd);
                        lv.Items.Refresh();

                        int tick4 = Environment.TickCount;

                        double elapsed2 = (tick4 - tick3) / 1000.0;

                        System.Windows.MessageBox.Show(elapsed2.ToString() + " seconds.");
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
        }
예제 #2
0
        private static void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection    direction;

            if (headerClicked != null)
            {
                if (headerClicked != _lastHeaderClicked)
                {
                    direction = ListSortDirection.Ascending;
                }

                else
                {
                    if (_lastDirection == ListSortDirection.Ascending)
                    {
                        direction = ListSortDirection.Descending;
                    }

                    else
                    {
                        direction = ListSortDirection.Ascending;
                    }
                }

                string header = String.Empty;

                try
                {
                    var binding = ((Binding)ListViewSorter.GetSortBindingMember(headerClicked.Column));
                    header = binding.Path.Path;
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }

                if (header == String.Empty)
                {
                    return;
                }

                Sort(header, direction);

                string       resourceTemplateName = String.Empty;
                DataTemplate tmpTemplate;

                //if (_lastHeaderClicked != null)
                //{
                //    resourceTemplateName = "HeaderTemplateSortNon";
                //    tmpTemplate = lv.TryFindResource(resourceTemplateName) as DataTemplate;
                //    _lastHeaderClicked.Column.HeaderTemplate = tmpTemplate;

                //}

                switch (direction)
                {
                case ListSortDirection.Ascending: resourceTemplateName = "HeaderTemplateSortAsc"; break;

                case ListSortDirection.Descending: resourceTemplateName = "HeaderTemplateSortDesc"; break;
                }

                tmpTemplate = lv.TryFindResource(resourceTemplateName) as DataTemplate;
                if (tmpTemplate != null)
                {
                    headerClicked.Column.HeaderTemplate = tmpTemplate;
                }

                _lastHeaderClicked = headerClicked;
                _lastDirection     = direction;
            }
        }