Microsoft’s PivotViewer control is an amazing tool for visualizing data and creating a unique UI for your application. The one issue everyone seems to have with it, however, is its lack of styling capability. In fact, working with PivotViewer in Expression Blend yields little in the way of styling or templating. There are only a few color settings one can make, and some generic overall changes. Replacing the filter panel or info panel is not currently supported.
What to do?
Since we cannot style the filter panel, we need to hide it and put our own in. Hiding the panel is relatively simple and straight-forward. Simply put, you need to subclass the PivotViewer and override the OnApplyTemplates() method, then walk the tree until you find the filter panel. Fortunately, tools like Snoop or Mole can help you determine where the filter panel resides. Below is a snippet of code showing where the filter panel resides in the Silverlight 5 version of the PivotViewer.
NOTE: This is very brittle code and will most likely fail when Microsoft releases a new version of the PivotViewer.
Now that the filter panel is gone, we need to create our own. If you’re manually creating the facets and data in code, then you should have no trouble binding a new Filter control to your data structures. But what if you’re using the CxmlCollectionSource and loading your data from a cxml external file?
As anyone who’s worked with the CxmlCollectionSource can tell you, the facets and their filter values aren’t readily available for consumption. In order to build up a collection of facets and filters, you to perform a little preprocessing. The Items collection off the CxmlCollectionSource is the dataset you’re working with, however, getting values and properties for the items isn’t immediately apparent.
My solution was to create a small class to hold the facet information I required:
In order to iterate through the Items collection, you need to wait until the CxmlCollectionSource finishes processing the .cxml document. Since it occurs asynchronously, you can start your processing in the event handler for the StateChanged event. That can look something like this (I’m not implementing an MVVM pattern for simplicity’s sake. This is just part of the code-behind for the MainPage.xaml).
The next step is to iterate over the Items of the CxmlCollectionSource and build a dictionary of Facets and their properties. The Items collection is a collection of PivotViewerItems that do not contain the actual values of the data elements. Instead, you need to call a method, GetPropertyValue(id) in order to get the list of properties that particular data elements contains.
The ProcessFacets() method below simply iterates over each item in the data set and determines which properties it contains that are filterable and what the acceptable values are for each. For example if your data has two filterable properties, such as “Ingredients” and “Serving Size”, these two facets could have a wide range of acceptable values for each data element. The call to GetPropertyValue for “Ingredients” for an particular item may return “Oil” and “Vinegar”, while another item may return “Milk”, “Oil” and “Peanuts.” The dictionary built by ProcessFacets will create a Key for “Ingredients” and add a List of four strings representing the four unique values.
I think it may be clearer just to read the code below:
So, there you have it.
Armed with this data, you can now create your own filter panel and programmatically control the PivotViewer independently of the built-in panel. All you would need to do from this point on is write the Xaml for your Filter view and bind the Facet data to it. Finally, call SetViewerState on the PivotViewer and pass in the filter string built by your new view. Note, however, that the filter string is a mini-language of a sorts and will require some processing in code to generate the appropriate parameter. To get you started, I iterate over the dictionary like so:
The PivotViewer is an extremely powerful tool to have in your arsenal, but it is not very customizable. With a small hack and some understanding of the data set, some of its limitations can be overcome. I hope this post gets you on the road to creating your own custom PivotViewer applications.