2

I have a Silverlight PivotViewer up and running, with about 4000 items.

I pass querystring parameters to the page that hosts the PivotViewer, to filter the items upon opening.

How do you add filters to the PivotViewer programmatically?

I've tried :

pvtTest.AppliedFilters.Add(new KeyValuePair<string, IList<string>>("Color", new List<string> { "EQ.Green", "EQ.Red" }));

but it still displays all the items. I've also tried checking what the value of the AppliedFilters property is after I set some filters using the UI, but it still only contains the filter that I added above.

Marcel
  • 944
  • 2
  • 9
  • 29
  • Found it... You can specify filters by using the ViewerState property. It has a specific syntax, which can be seen here : http://www.silverlight.net/content/pivotviewer/developer-info/api/html/P_System_Windows_Pivot_PivotViewer_ViewerState.htm – Marcel Sep 09 '11 at 07:16

3 Answers3

2

For PivotViewer-2

string sFilter = pvViewer.Filter; //get existing filter

pvViewer.ItemsSource = new-data-source; //assign new data source

pvViewer.Filter = sFilter; //reassign filter
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Aishu
  • 413
  • 1
  • 5
  • 20
  • Crap, this works - thanks. I spent basically a full day trying to figure this out, and had basically given up. – McGarnagle Dec 20 '12 at 22:05
1

I think the answer by grimstoner might not be clear enough.

You can indeed use the ViewerState property to set filters, but you cannot set it directly as it only has a public getter. The workaround is to use the LoadCollection() method passing the URI of the already loaded collection and the modified viewer state. You might expect that the Pivot Viewer will reload the collection, but this is not the case. It seems smart enough to detect the identical URI and only applies the new viewer state.

Example:

string viewerState = pivotViewer.ViewerState;

// Modify the viewer state according to the rules defined at:
// http://www.silverlight.net/content/pivotviewer/developer-info/api/html/P_System_Windows_Pivot_PivotViewer_ViewerState.htm

pivotViewer.LoadCollection(pivotViewer.CollectionUri.ToString(), viewerState);

I hope this helps others with the same question.

CodeZombie
  • 5,367
  • 3
  • 30
  • 37
  • Note that this doesn't work for the latest version of Pivot Viewer. There's no `LoadCollection` method: http://msdn.microsoft.com/en-us/library/system.windows.controls.pivot.pivotviewer(v=vs.95).aspx – McGarnagle Dec 20 '12 at 22:06
1

You can specify filters by using the ViewerState property.

It has a very specific syntax, which can be found here.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Marcel
  • 944
  • 2
  • 9
  • 29