Sorting a Pivot Viewer 2.0’s Group Items

by jasonrshaver 23. January 2012 13:54

Let’s just say you want to make a PivotViewer for the Periodic Table of Elements.  And lets just say you want it to look something like you remember it in your middle school science book.  You get the data, add a ‘Group’ value to control the columns, but the vertical order is all wrong. 

Here is a quick way to fix this.  I don’t think anyone documented the method for doing this before, so if you have any questions, let me know!

And add the following to your PivotViewer XAML

<pv:PivotViewer x:Name="myPivotViewer" ItemsSource="{Binding}" BorderThickness="0" ViewChanged="myPivotViewer_ViewChanged">

And in code behind, add:

  1. private System.Windows.Controls.Pivot.PivotViewerView _OldView = null;
  2.  
  3. private void myPivotViewer_ViewChanged(object sender, EventArgs e)
  4. {
  5.     if (_OldView != null)
  6.     {
  7.         _OldView.PivotViewerViewUpdating -=
  8.             new EventHandler<PivotViewerViewUpdatingEventArgs>
  9.                 (View_PivotViewerViewUpdating);
  10.     }
  11.     _OldView = myPivotViewer.View;
  12.     _OldView.PivotViewerViewUpdating +=
  13.         new EventHandler<PivotViewerViewUpdatingEventArgs>
  14.             (View_PivotViewerViewUpdating);
  15. }
  16.  
  17. private void View_PivotViewerViewUpdating(object sender
  18.     , PivotViewerViewUpdatingEventArgs e)
  19. {
  20.     e.CollectionView.SortDescriptions.Add(
  21.         new SortDescription("AtomicNumber"
  22.             , ListSortDirection.Ascending));
  23. }

It is very important to make sure you are cleaning up your PivotViewerViewUpdating event reference as shown!

And Ta-Da, you have the following work of art:

clip_image002

Sorry for making such a quick example without any downloadable source, but I figured it was good to get this information out there!

Tags: ,

Blog

About the author

I am a software developer working for Microsoft in Redmond, WA.  In addition, my wife and I own TTXOnline, what is likely the 3rd largest table tennis store in the US.

Month List

Page List