Ejemplo n.º 1
0
        public static SortInfo GetSort(ListView listView)
        {
            SortInfo sort = new SortInfo();

            if (listView.Items.SortDescriptions.Count > 0)
            {
                sort.PropertyName = listView.Items.SortDescriptions[0].PropertyName;
                sort.Direction    = listView.Items.SortDescriptions[0].Direction;
                GridViewColumnHeader currentSortedColumnHeader = GridViewSort.GetSortedColumnHeader(listView);
                sort.ColumnName    = currentSortedColumnHeader.Content.ToString();
                sort.SelectedIndex = listView.SelectedIndex;
            }
            return(sort);
        }
Ejemplo n.º 2
0
        public static void SetSort(ListView listView, SortInfo sort)
        {
            if (string.IsNullOrEmpty(sort.PropertyName))
            {
                return;
            }

            SortInfo currentSort = GetSort(listView);
            int      number;

            if (string.IsNullOrEmpty(currentSort.PropertyName) ||
                0 != string.CompareOrdinal(currentSort.ColumnName, sort.ColumnName))
            { // different column
                number = sort.Direction == ListSortDirection.Descending ? 2 : 1;
            }
            else
            { // same column
                if (currentSort.Direction == sort.Direction)
                {
                    return;
                }
                number = 1;
            }

            for (int i = 0; i < number; i++)
            {
                // programmatically click column header number of times
                GridViewSort.ApplySort(listView.Items, sort.PropertyName, listView, GetHeaderColimn(listView, sort.ColumnName));
            }

            listView.SelectedIndex = sort.SelectedIndex != -1 ? sort.SelectedIndex : 0;

            // refocus list view
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                ListViewItem item = listView.ItemContainerGenerator.ContainerFromIndex(listView.SelectedIndex) as ListViewItem;
                item?.Focus();
            }));
        }