Version

Including/Excluding Metadata Tree Items (xamPivotGrid, xamPivotDataSelector)

Topic Overview

Purpose

When you assign a data source to the xamPivotDataSelector™ it will automatically add all items (Measures, Dimensions, Hierarchies, etc.) in the data source to the metadata tree. This topic explains how you can choose which items are included in the metadata tree and which are not.

Required background

The following table lists the topics required as a prerequisite to understanding this topic.

Topic Purpose

This section serves as an introduction to the xamPivotGrid control’s key features and functionalities. The topics listed here will give you a better idea of why you would want to use xamPivotGrid in your applications.

This topic demonstrates how to get started with the xamPivotGrid™ control by providing step-by-step procedure for adding this control to a WPF application.

This topic is an introduction to the xamPivotDataSelector control.

Including/Excluding items from the xamPivotDataSelector’s Metadata Tree

Introduction

Controlling which items are added to the metadata tree in the xamPivotDataSelector happens through the data source that is assigned to its DataSource property. This is achieved by subscribing for the MetadataTreeItemAdding event. Additionally the ResetMetadataTree method may be used.

When the data selector is being initialized, the MetadataTreeItemAdding event is raised every time before an item is added to the metadata tree of the data selector. The MetadataTreeEventArgs instance that is provided with the event contains a reference to the item being added and a Cancel property through which adding of the item can be suspended. Apart from expecting the Item that is being added and deciding whether you want to include it in the metadata tree or not, in the event handler for the MetadataTreeItemAdding event, you can also specify if an item should be expanded after it is initialized.

If you want to modify the items in the xamPivotDataSelector’s metadata tree after it has been initialized you can call the ResetMetadataTree method. This will cause all items in the data source to be re-added to the metadata tree, raising the MetadataTreeItemAdding event for each of them again.

Preview

The following screenshot is a preview of a xamPivotDataSelector where the only items added in the metadata tree are the AmountOfSale measure and the Seller and City hierarchies. The data source used is from the SalesDataSample class.

xamPivotGrid DataSelector Including Excluding Items form the MetaDataTree.png

Requirements

To complete the procedure, you need the WPF application where the xamPivotGrid and the xamPivotDataSelector controls have been configured with the SalesDataSample class as data source.

Overview

This topic takes you step-by-step toward creating an event handler for the MetadataTreeItemAdding event where only items that meet some predefined criteria are added to the metadata tree. The following is a conceptual overview of the process: 1 . Adding an event handler for the MetadataTreeItemAdding event.

Inspecting the item that is being added and deciding whether it should be included or not.

Steps

The following steps demonstrate how to use the MetadataTreeItemAdding event in order to add only a list of predefined items to the metadata tree.

  1. Add an event handler for the MetadataTreeItemAdding event.

Normally you would subscribe to this event before the page is loaded e.g. in its constructor. In this way you will ensure that the event will be raised when the xamPivotDataSelector is initializing.

Alternatively you can subscribe to this event at any time later before calling the ResetMetadataTree method.

  1. Inspect the item that is being added and decide whether you want to include it or not.

As mentioned earlier the event arguments supplied with the occurrence of the MetadataTreeItemAdding event contain a reference to the actual item being added. You can expect the values of its ItemType and Caption properties, but if this is not enough to decide whether it should be included, you can expect the underlying DataObject which could be a Hierarchy, Dimension, Level, etc.

Code Example: Including/Excluding Items from the xamPivotDataSelector’s Metadata Tree

Description

The code below is an example of how only specific items can be added to the data selector’s metadata tree.

Code

In C#:

Code
using System.Windows.Controls;
using Infragistics.Olap;
using Infragistics.Olap.FlatData;
using Infragistics.Olap.Data;
using Infragistics.Samples.Data.Models;
using System.Linq;
…
// Place this in the constructor of the page.
            ((FlatDataSource)dataSelector.DataSource).MetadataTreeItemAdding += DataSelector_MetadataTreeItemAdding;
…
private static string[] allowedMeasureNames = { "Measures", "AmountOfSale" };
private static string[] allowedDimensionNames = { "[Seller]", "[City]" };
private static string[] allowedHierarchyNames = { "[Seller].[Seller]", "[City].[City]" };
void DataSelector_MetadataTreeItemAdding(object sender, Infragistics.Olap.MetadataTreeEventArgs e)
{
    if (e.Item.ItemType == ItemTypes.Measure &&
        allowedMeasureNames.Contains(e.Item.Caption))
        return;
    if (e.Item.ItemType == ItemTypes.Dimension
        && allowedDimensionNames.Contains(((IDimension)e.Item.DataObject).UniqueName))
    {
        e.Item.ExpandWhenInitialized = true;
        return;
    }
    if (e.Item.ItemType == ItemTypes.UserDefinedHierarchy
        && allowedHierarchyNames.Contains(((IHierarchy)e.Item.DataObject).UniqueName))
        return;
    e.Cancel = true;
}
// If you want to reset the metadata tree and call the following method:
// ((DataSourceBase)this.pivotGrid.DataSource).ResetMetadataTree();

In Visual Basic:

Imports System.Windows.Controls
Imports Infragistics.Olap
Imports Infragistics.Olap.FlatData
Imports Infragistics.Olap.Data
Imports Infragistics.Samples.Data.Models
Imports System.Linq
…
' Place this in the constructor of the page.
AddHandler DirectCast(dataSelector.DataSource, FlatDataSource).MetadataTreeItemAdding, AddressOf DataSelector_MetadataTreeItemAdding
…
Private Shared allowedMeasureNames As String() = {"Measures", "AmountOfSale"}
Private Shared allowedDimensionNames As String() = {"[Seller]", "[City]"}
Private Shared allowedHierarchyNames As String() = {"[Seller].[Seller]", "[City].[City]"}
Private Sub DataSelector_MetadataTreeItemAdding(sender As Object, e As Infragistics.Olap.MetadataTreeEventArgs)
      If e.Item.ItemType = ItemTypes.Measure AndAlso allowedMeasureNames.Contains(e.Item.Caption) Then
            Return
      End If
      If e.Item.ItemType = ItemTypes.Dimension AndAlso allowedDimensionNames.Contains(DirectCast(e.Item.DataObject, IDimension).UniqueName) Then
            e.Item.ExpandWhenInitialized = True
            Return
      End If
      If e.Item.ItemType = ItemTypes.UserDefinedHierarchy AndAlso allowedHierarchyNames.Contains(DirectCast(e.Item.DataObject, IHierarchy).UniqueName) Then
            Return
      End If
      e.Cancel = True
End Sub
' If you want to reset the metadata tree and call the following method:
' DirectCast(dataSelector.DataSource, DataSourceBase).ResetMetadataTree()