This example shows how to put field and group definition logic in the ViewModel and setup the Pivot Grid Control.
using System.Collections.ObjectModel;
using DevExpress.Xpf.PivotGrid;
using PivotGridViewModelBinding.nwindDataSetTableAdapters;
namespace PivotGridViewModelBinding {
public class ViewModel {
// The collection of Pivot Grid fields.
public ObservableCollection<Field> Fields { get; private set; }
// The collection of Pivot Grid groups.
public ObservableCollection<FieldGroup> Groups { get; private set; }
// The view model that contains the fields and groups settings.
public ViewModel() {
Fields = new ObservableCollection<Field>() {
new Field() { FieldName="Country", AreaIndex=0, FieldArea = FieldArea.RowArea, Name="fieldCountry",
GroupName="groupSalesPerson", GroupIndex=0},
new Field() { FieldName="Sales Person", AreaIndex=1, FieldArea = FieldArea.RowArea, Name="fieldSalesPerson",
GroupName="groupSalesPerson", GroupIndex=1},
new Field() { FieldName="OrderDate", AreaIndex=0, FieldArea = FieldArea.ColumnArea, Name="fieldOrderYear",
Interval = FieldGroupInterval.DateYear, FieldCaption = "Year", GroupName="groupYearMonth", GroupIndex=0 },
new Field() { FieldName="OrderDate", AreaIndex=1, FieldArea = FieldArea.ColumnArea, Name="fieldOrderMonth",
Interval = FieldGroupInterval.DateMonth, FieldCaption = "Month", GroupName="groupYearMonth", GroupIndex=1 },
new Field() { FieldName="Extended Price", AreaIndex=0, FieldArea = FieldArea.DataArea, Name="fieldPrice" },
};
Groups = new ObservableCollection<FieldGroup>() {
new FieldGroup() { GroupName = "groupYearMonth" },
new FieldGroup() { GroupName = "groupSalesPerson" }
};
salesPersonDataAdapter.Fill(salesPersonDataTable);
}
nwindDataSet.SalesPersonDataTable salesPersonDataTable = new nwindDataSet.SalesPersonDataTable();
SalesPersonTableAdapter salesPersonDataAdapter = new SalesPersonTableAdapter();
public nwindDataSet.SalesPersonDataTable DataSource { get { return salesPersonDataTable; } }
}
public class Field {
public string FieldName { get; set; }
public string Name { get; set; }
public string FieldCaption { get; set; }
public FieldArea FieldArea { get; set; }
public int AreaIndex { get; set; }
public FieldGroupInterval Interval { get; set; }
public string GroupName { get; set; }
public int GroupIndex { get; set; }
}
public class FieldGroup {
public string GroupName { get; set; }
}
}
using DevExpress.Xpf.PivotGrid;
using System.Windows;
using System.Windows.Controls;
namespace PivotGridViewModelBinding {
public class FieldTemplateSelector : DataTemplateSelector {
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
Field field = (Field)item;
if(field.Interval == FieldGroupInterval.DateMonth || field.Interval == FieldGroupInterval.DateYear) {
return (DataTemplate)((Control)container).FindResource("IntervalFieldTemplate");
}
return (DataTemplate)((Control)container).FindResource("DefaultFieldTemplate");
}
}
}
<Window x:Class="PivotGridViewModelBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PivotGridViewModelBinding"
xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxci="http://schemas.devexpress.com/winfx/2008/xaml/core/internal"
xmlns:nwindDataSetTableAdapters="clr-namespace:PivotGridViewModelBinding.nwindDataSetTableAdapters"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
x:Name="Form1"
Title="MainWindow" Height="362" Width="627"
DataContext="{dxmvvm:ViewModelSource Type=local:ViewModel}">
<Window.Resources>
<local:FieldTemplateSelector x:Key="FieldTemplateSelector" />
<dx:TypedSimpleSource x:Key="TypedSimpleSource" AdapterType="{x:Type nwindDataSetTableAdapters:SalesPersonTableAdapter}"
ContextType="{x:Type local:nwindDataSet}" Path="SalesPerson" />
<DataTemplate x:Key="DefaultFieldTemplate">
<ContentControl>
<dxpg:PivotGridField FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"
Area="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldArea, RelativeSource={RelativeSource Self}}"
AreaIndex="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).AreaIndex, RelativeSource={RelativeSource Self}}"
Caption="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldCaption, RelativeSource={RelativeSource Self}}"
GroupName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).GroupName, RelativeSource={RelativeSource Self}}"
GroupIndex="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).GroupIndex, RelativeSource={RelativeSource Self}}"
dx:XamlHelper.Name="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Name, RelativeSource={RelativeSource Self}}"
>
</dxpg:PivotGridField>
</ContentControl>
</DataTemplate>
<DataTemplate x:Key="IntervalFieldTemplate">
<ContentControl>
<dxpg:PivotGridField FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"
Area="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldArea, RelativeSource={RelativeSource Self}}"
AreaIndex="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).AreaIndex, RelativeSource={RelativeSource Self}}"
Caption="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldCaption, RelativeSource={RelativeSource Self}}"
GroupName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).GroupName, RelativeSource={RelativeSource Self}}"
GroupIndex="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).GroupIndex, RelativeSource={RelativeSource Self}}"
GroupInterval="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Interval, RelativeSource={RelativeSource Self}}"
dx:XamlHelper.Name="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Name, RelativeSource={RelativeSource Self}}"
HeaderImage="{dx:DXImage Image=Calendar_16x16.png}">
</dxpg:PivotGridField>
</ContentControl>
</DataTemplate>
<DataTemplate x:Key="GroupTemplate">
<ContentControl>
<dxpg:PivotGridGroup
dx:XamlHelper.Name="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).GroupName, RelativeSource={RelativeSource Self}}">
</dxpg:PivotGridGroup>
</ContentControl>
</DataTemplate>
</Window.Resources>
<Grid>
<dxpg:PivotGridControl x:Name="pivotGridControl1"
DataSource="{Binding DataSource}"
FieldsSource="{Binding Fields }"
FieldGeneratorTemplateSelector="{StaticResource FieldTemplateSelector}"
GroupsSource="{Binding Groups}"
GroupGeneratorTemplate="{StaticResource GroupTemplate}"/>
</Grid>
</Window>
Imports DevExpress.Xpf.PivotGrid
Imports System.Windows
Imports System.Windows.Controls
Namespace PivotGridViewModelBinding
Public Class FieldTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
Dim field As Field = DirectCast(item, Field)
If field.Interval = FieldGroupInterval.DateMonth OrElse field.Interval = FieldGroupInterval.DateYear Then
Return DirectCast(CType(container, Control).FindResource("IntervalFieldTemplate"), DataTemplate)
End If
Return DirectCast(CType(container, Control).FindResource("DefaultFieldTemplate"), DataTemplate)
End Function
End Class
End Namespace
Imports System.Collections.ObjectModel
Imports DevExpress.Xpf.PivotGrid
Namespace PivotGridViewModelBinding
Public Class ViewModel
' The collection of Pivot Grid fields.
Private privateFields As ObservableCollection(Of Field)
Public Property Fields() As ObservableCollection(Of Field)
Get
Return privateFields
End Get
Private Set(ByVal value As ObservableCollection(Of Field))
privateFields = value
End Set
End Property
' The collection of Pivot Grid groups.
Private privateGroups As ObservableCollection(Of FieldGroup)
Public Property Groups() As ObservableCollection(Of FieldGroup)
Get
Return privateGroups
End Get
Private Set(ByVal value As ObservableCollection(Of FieldGroup))
privateGroups = value
End Set
End Property
' The view model that contains the fields and groups settings.
Public Sub New()
Fields = New ObservableCollection(Of Field)() From {
New Field() With {
.FieldName = "Country",
.AreaIndex = 0,
.FieldArea = FieldArea.RowArea,
.Name = "fieldCountry",
.GroupName = "groupSalesPerson",
.GroupIndex = 0
},
New Field() With {
.FieldName = "Sales Person",
.AreaIndex = 1,
.FieldArea = FieldArea.RowArea,
.Name = "fieldSalesPerson",
.GroupName = "groupSalesPerson",
.GroupIndex = 1
},
New Field() With {
.FieldName = "OrderDate",
.AreaIndex = 0,
.FieldArea = FieldArea.ColumnArea,
.Name = "fieldOrderYear",
.Interval = FieldGroupInterval.DateYear,
.FieldCaption = "Year",
.GroupName = "groupYearMonth",
.GroupIndex = 0
},
New Field() With {
.FieldName = "OrderDate",
.AreaIndex = 1,
.FieldArea = FieldArea.ColumnArea,
.Name = "fieldOrderMonth",
.Interval = FieldGroupInterval.DateMonth,
.FieldCaption = "Month",
.GroupName = "groupYearMonth",
.GroupIndex = 1
},
New Field() With {
.FieldName = "Extended Price",
.AreaIndex = 0,
.FieldArea = FieldArea.DataArea,
.Name = "fieldPrice"
}
}
Groups = New ObservableCollection(Of FieldGroup)() From {
New FieldGroup() With {.GroupName = "groupYearMonth"},
New FieldGroup() With {.GroupName = "groupSalesPerson"}
}
salesPersonDataAdapter.Fill(salesPersonDataTable)
End Sub
Private salesPersonDataTable As New nwindDataSet.SalesPersonDataTable()
Private salesPersonDataAdapter As New nwindDataSetTableAdapters.SalesPersonTableAdapter()
Public ReadOnly Property DataSource() As nwindDataSet.SalesPersonDataTable
Get
Return salesPersonDataTable
End Get
End Property
End Class
Public Class Field
Public Property FieldName() As String
Public Property Name() As String
Public Property FieldCaption() As String
Public Property FieldArea() As FieldArea
Public Property AreaIndex() As Integer
Public Property Interval() As FieldGroupInterval
Public Property GroupName() As String
Public Property GroupIndex() As Integer
End Class
Public Class FieldGroup
Public Property GroupName() As String
End Class
End Namespace