private void GridViewColumnHeader_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { SortDescriptionCollection sortDescriptions = View.SortDescriptions; Image columnSortImage = ((Image)((StackPanel)((GridViewColumnHeader)sender).Content).Children[0]); object columnContent = ((GridViewColumnHeader)sender).Content; foreach (GridViewColumn column in ((GridView)listViewMain.View).Columns) { if (((GridViewColumnHeader)column.Header).Content.Equals(columnContent)) { string propertyName = column.DisplayMemberBinding == null ? "PriorityInt" : ((Binding)column.DisplayMemberBinding).Path.Path; bool contained = false; int position = 0; for (int i = 0; i < sortDescriptions.Count; i++) { if (sortDescriptions[i].PropertyName.Equals(propertyName)) { contained = true; position = i; break; } } if (!contained) { sortDescriptions.Add(new SortDescription(propertyName, ListSortDirection.Ascending)); columnSortImage.Source = new BitmapImage(new Uri(@"/Resources/Images/SortedAscending.png", UriKind.Relative)); } else { if (sortDescriptions[position].Direction == ListSortDirection.Ascending) { sortDescriptions.RemoveAt(position); sortDescriptions.Insert(position, new SortDescription(propertyName, ListSortDirection.Descending)); columnSortImage.Source = new BitmapImage(new Uri(@"/Resources/Images/SortedDescending.png", UriKind.Relative)); } else { sortDescriptions.RemoveAt(position); columnSortImage.Source = new BitmapImage(new Uri(@"/Resources/Images/Unsorted.png", UriKind.Relative)); } } } } } }
public void SortDescriptionCollectionRemoveTest() { SortDescriptionCollection sdc = new SortDescriptionCollection(); SortDescription removedItem = new SortDescription("NONE", ListSortDirection.Ascending); sdc.Add(new SortDescription("A", ListSortDirection.Ascending)); ((INotifyCollectionChanged)sdc).CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e) { Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action, "REM_#0"); removedItem = (SortDescription)e.OldItems [0]; Assert.AreEqual(true, removedItem.IsSealed, "REM_#0b"); }; sdc.RemoveAt(0); Assert.AreEqual("A", removedItem.PropertyName, "REM_#1"); Assert.AreEqual(ListSortDirection.Ascending, removedItem.Direction, "REM_#2"); Assert.AreEqual(true, removedItem.IsSealed, "REM_#3"); }
// keep inner and outer CollViews' SortDescription collections in synch private void SynchronizeSortDescriptions(NotifyCollectionChangedEventArgs e, SortDescriptionCollection origin, SortDescriptionCollection clone) { if (clone == null) return; // the clone might be lazily-created _sort switch (e.Action) { case NotifyCollectionChangedAction.Add: Debug.Assert(e.NewStartingIndex >= 0); if (clone.Count + e.NewItems.Count != origin.Count) goto case NotifyCollectionChangedAction.Reset; for (int i = 0; i < e.NewItems.Count; i++) { clone.Insert(e.NewStartingIndex + i, (SortDescription) e.NewItems[i]); } break; case NotifyCollectionChangedAction.Remove: if (clone.Count - e.OldItems.Count != origin.Count) goto case NotifyCollectionChangedAction.Reset; Debug.Assert(e.OldStartingIndex >= 0); for (int i = 0; i < e.OldItems.Count; i++) { clone.RemoveAt(e.OldStartingIndex); } break; case NotifyCollectionChangedAction.Replace: Debug.Assert(e.OldStartingIndex >= 0); if (clone.Count != origin.Count) goto case NotifyCollectionChangedAction.Reset; for (int i = 0; i < e.OldItems.Count; i++) { clone[e.OldStartingIndex + i] = (SortDescription) e.NewItems[i]; } break; case NotifyCollectionChangedAction.Move: Debug.Assert(e.OldStartingIndex >= 0); if (clone.Count != origin.Count) goto case NotifyCollectionChangedAction.Reset; if (e.NewItems.Count == 1) { clone.RemoveAt(e.OldStartingIndex); clone.Insert(e.NewStartingIndex, (SortDescription) e.NewItems[0]); } else { for (int i = 0; i < e.OldItems.Count; i++) { clone.RemoveAt(e.OldStartingIndex); } for (int i = 0; i < e.NewItems.Count; i++) { clone.Insert(e.NewStartingIndex + i, (SortDescription) e.NewItems[i]); } } break; // this arm also handles cases where the two collections have gotten // out of [....] (typically because exceptions prevented a previous [....] // from happening) case NotifyCollectionChangedAction.Reset: CloneList(clone, origin); break; default: throw new NotSupportedException(SR.Get(SRID.UnexpectedCollectionChangeAction, e.Action)); } }
public void SortDescriptionCollectionRemoveTest() { SortDescriptionCollection sdc = new SortDescriptionCollection (); SortDescription removedItem = new SortDescription ("NONE", ListSortDirection.Ascending); sdc.Add (new SortDescription ("A", ListSortDirection.Ascending)); ((INotifyCollectionChanged)sdc).CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) { Assert.AreEqual (NotifyCollectionChangedAction.Remove, e.Action, "REM_#0"); removedItem = (SortDescription)e.OldItems [0]; Assert.AreEqual (true, removedItem.IsSealed, "REM_#0b"); }; sdc.RemoveAt (0); Assert.AreEqual ("A", removedItem.PropertyName, "REM_#1"); Assert.AreEqual (ListSortDirection.Ascending, removedItem.Direction, "REM_#2"); Assert.AreEqual (true, removedItem.IsSealed, "REM_#3"); }
private void SynchronizeDetailSortDescriptions( SortDescriptionCollection masterSortDescriptions, SortDescriptionCollection detailSortDescriptions, FieldNameMap itemPropertyMap ) { ColumnSortCommand.ThrowIfNull( masterSortDescriptions, "masterSortDescriptions" ); ColumnSortCommand.ThrowIfNull( detailSortDescriptions, "detailSortDescriptions" ); ColumnSortCommand.ThrowIfNull( itemPropertyMap, "itemPropertyMap" ); int masterSortDescriptionsCount = masterSortDescriptions.Count; if( masterSortDescriptionsCount > 0 ) { int insertionIndex = 0; for( int i = 0; i < masterSortDescriptionsCount; i++ ) { var sortDescription = masterSortDescriptions[ i ]; string detailPropertyName; if( !itemPropertyMap.TryGetItemPropertyName( sortDescription.PropertyName, out detailPropertyName ) ) continue; var detailDirection = sortDescription.Direction; if( insertionIndex < detailSortDescriptions.Count ) { var detailSortDescription = detailSortDescriptions[ insertionIndex ]; if( ( detailSortDescription.PropertyName != detailPropertyName ) || ( detailSortDescription.Direction != detailDirection ) ) { detailSortDescriptions[ insertionIndex ] = new SortDescription( detailPropertyName, detailDirection ); } } else { detailSortDescriptions.Add( new SortDescription( detailPropertyName, detailDirection ) ); } insertionIndex++; } while( insertionIndex < detailSortDescriptions.Count ) { detailSortDescriptions.RemoveAt( insertionIndex ); } } else if( detailSortDescriptions.Count > 0 ) { detailSortDescriptions.Clear(); } }