private void SortSwatches(ColorSortType sortType, ListSortDirection sortDirection = ListSortDirection.Ascending) { int sd = sortDirection.Equals(ListSortDirection.Ascending) ? 1 : -1; switch (sortType) { case ColorSortType.Luminocity: Swatches.Sort((a, b) => { var aa = a.Color.Luminosity; var bb = b.Color.Luminosity; return(sd * aa.CompareTo(bb)); }); break; case ColorSortType.Saturation: Swatches.Sort((a, b) => sd * a.Color.Saturation.CompareTo(b.Color.Saturation)); break; case ColorSortType.Hue: Swatches.Sort((a, b) => { var aa = a.Color.Hue + a.Color.Luminosity; var bb = b.Color.Hue + b.Color.Luminosity; return(sd * aa.CompareTo(bb)); }); Swatches.Sort((a, b) => sd * a.Color.Hue.CompareTo(b.Color.Hue)); break; case ColorSortType.Name: Swatches.Sort((a, b) => sd * a.Name.CompareTo(b.Name)); break; } }
private void SortClick(object sender, RoutedEventArgs e) { try { GridViewColumnHeader column = sender as GridViewColumnHeader; String field = column.Tag as String; DataView dv = (DataView)lstAnalysisDetails.DataContext; if (_CurSortCol != null) { AdornerLayer.GetAdornerLayer(_CurSortCol).Remove(_CurAdorner); lstAnalysisDetails.Items.SortDescriptions.Clear(); } ListSortDirection newDir = ListSortDirection.Ascending; if (_CurSortCol == column && _CurAdorner.Direction == newDir) { newDir = ListSortDirection.Descending; } dv.Sort = "SortColumn desc, " + field + (newDir.Equals(ListSortDirection.Ascending) ? " asc" : " desc"); lstAnalysisDetails.DataContext = dv; _CurSortCol = column; _CurAdorner = new SortAdorner(_CurSortCol, newDir); AdornerLayer.GetAdornerLayer(_CurSortCol).Add(_CurAdorner); //lstAnalysisDetails.Items.SortDescriptions.Add(new SortDescription(field, newDir)); } catch (Exception ex) { ExceptionManager.Publish(ex); } }
public int Compare(ProgramInfo x, ProgramInfo y) { try { var priority = 0; switch ((string)_column.Header) { case "Program": priority = string.CompareOrdinal(x.Program, y.Program); break; case "Publisher": priority = string.CompareOrdinal(x.Publisher, y.Publisher); break; case "Size": priority = x.SizeBytes.CompareTo(y.SizeBytes); break; } return(_direction.Equals(ListSortDirection.Ascending) ? priority : -priority); } catch { return(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; } }
/// <summary> /// Returns "ASC" or "DESC". /// </summary> internal static string ToBindingSourceSortString(this ListSortDirection direction) { return(direction.Equals(ListSortDirection.Descending) ? "DESC" : "ASC"); }
public int Compare(object x, object y) { int comp = (x as MItem)[name].CompareTo((y as MItem)[name]); return(lsd.Equals(ListSortDirection.Ascending) ? comp : comp * -1); }
public static string ToDirectionString(this ListSortDirection direction) { return(direction.Equals(ListSortDirection.Descending) ? "DESC" : "ASC"); }