我在WPF中有一个PivotGrid(DevExpress),并将数据源绑定到一个IList,该IList包含具有DateTime类型的多个属性的对象。我希望最终用户在运行时选择用户要按年,月或日分组的那些DateTime字段。那可能吗?
我知道我可以通过编程方式提供DateTime分组,但是由于有多个DateTime字段,因此如果最终用户可以选择要对哪个DateTime字段进行分组以及如何在运行时对其进行分组,则将非常繁琐和不必要。
你能指导我怎么做吗?
我有以下几点:
<dxdo:LayoutControlItem ItemWidth="1*">
<dxpg:PivotGridControl MaxHeight="800" MaxWidth="800" DataSource="{Binding AllChildOrders}" DataSourceChanged="PivotGridControl_OnDataSourceChanged">
</dxpg:PivotGridControl>
</dxdo:LayoutControlItem>
并在后面的代码中:
private void PivotGridControl_OnDataSourceChanged(object sender, RoutedEventArgs e)
{
var pivotTable = sender as PivotGridControl;
pivotTable.RetrieveFields();
}
上面的代码有效,并且数据透视表将在运行时显示所有可用字段,包括类型为DateTime的字段。我不想以编程方式指定要以特定方式分组的字段,而是让最终用户在运行时选择如何分组的字段。可能的?
或者,我可以想象以编程方式创建子分组,如下所示:如何完成以下任务?
0.预先生成的组
如果您不想以编程方式指定要分组的字段,则可以为每个DateTime
字段预先生成组,以便用户可以在字段本身和字段组之间进行选择。
这是示例:
private void PivotGridControl_DataSourceChanged(object sender, RoutedEventArgs e)
{
var pivotTable = sender as PivotGridControl;
pivotTable.Groups.Clear();
pivotTable.RetrieveFields();
var dateTimeFields = pivotTable.Fields.Where(item => item.DataType == typeof(DateTime)).ToList();
foreach (var field in dateTimeFields)
{
var group = new PivotGridGroup();
group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (year)", GroupInterval = FieldGroupInterval.DateYear });
group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (month)", GroupInterval = FieldGroupInterval.DateMonth });
group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (day)", GroupInterval = FieldGroupInterval.DateDay });
foreach (var groupField in group)
pivotTable.Fields.Add(groupField);
pivotTable.Groups.Add(group);
}
}
这是示例的屏幕截图:
1.创建子分组
您可以使用PivotGridField.DisplayFolder
属性创建子分组。
这是示例:
private void PivotGridControl_DataSourceChanged(object sender, RoutedEventArgs e)
{
var pivotTable = sender as PivotGridControl;
pivotTable.RetrieveFields();
var dateTimeFields = pivotTable.Fields.Where(item => item.DataType == typeof(DateTime)).ToList();
foreach (var field in dateTimeFields)
{
var fieldYear = new PivotGridField()
{
FieldName = field.FieldName,
Caption = field.Caption + " (year)",
GroupInterval = FieldGroupInterval.DateYear,
Visible = false,
DisplayFolder = field.Caption
};
var fieldMonth = new PivotGridField()
{
FieldName = field.FieldName,
Caption = field.Caption + " (month)",
GroupInterval = FieldGroupInterval.DateMonth,
Visible = false,
DisplayFolder = field.Caption
};
var fieldDay = new PivotGridField()
{
FieldName = field.FieldName,
Caption = field.Caption + " (day)",
GroupInterval = FieldGroupInterval.DateDay,
Visible = false,
DisplayFolder = field.Caption
};
pivotTable.Fields.Add(fieldYear);
pivotTable.Fields.Add(fieldMonth);
pivotTable.Fields.Add(fieldDay);
}
}
结果如下:
2.自定义弹出菜单
您可以将命令添加到字段弹出菜单,该菜单允许用户更改组间隔。为此,您可以使用PivotGridControl.PopupMenuShowing
事件和PopupMenuShowingEventArgs.Customizations
属性来自定义菜单。
这是示例:
private void PivotGridControl_DataSourceChanged(object sender, RoutedEventArgs e)
{
var pivotTable = sender as PivotGridControl;
pivotTable.Groups.Clear();
pivotTable.RetrieveFields();
}
private void PivotGridControl_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
if (e.MenuType != PivotGridMenuType.Header)
return;
var fieldHeader = e.TargetElement as FieldHeader;
if (fieldHeader == null)
return;
var field = fieldHeader.Content as PivotGridField;
if (field == null || (field.Group != null && field.Group.IndexOf(field) > 0))
return;
var groupInterval = field.GroupInterval;
if (groupInterval == FieldGroupInterval.Default && field.DataType != typeof(DateTime))
return;
var dateTimeIntervals = new List<FieldGroupInterval>(new FieldGroupInterval[]
{
FieldGroupInterval.DateYear,
FieldGroupInterval.DateQuarter,
FieldGroupInterval.DateMonth,
FieldGroupInterval.DateDay,
FieldGroupInterval.Hour,
FieldGroupInterval.Minute,
FieldGroupInterval.Second,
FieldGroupInterval.DateWeekOfYear,
FieldGroupInterval.DateWeekOfMonth,
FieldGroupInterval.DateDayOfYear,
FieldGroupInterval.DateDayOfWeek,
FieldGroupInterval.Date,
FieldGroupInterval.Default
});
if (!dateTimeIntervals.Contains(groupInterval))
return;
var pivotTable = sender as PivotGridControl;
var subMenu = new BarSubItem() { };
subMenu.Content = "Set group interval";
if (field.Group == null)
{
var button = new BarButtonItem() { Content = "Year - Month - Date" };
button.ItemClick += (s, eventArgs) =>
{
pivotTable.BeginUpdate();
var group = field.Tag as PivotGridGroup;
if (group == null)
{
if (groupInterval != FieldGroupInterval.Default)
field.Caption = field.Caption.Replace(" (" + groupInterval + ")", string.Empty);
group = new PivotGridGroup();
group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (year)", GroupInterval = FieldGroupInterval.DateYear, Tag = field, Area = field.Area, AreaIndex = field.AreaIndex });
group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (month)", GroupInterval = FieldGroupInterval.DateMonth });
group.Add(new PivotGridField() { FieldName = field.FieldName, Caption = field.Caption + " (day)", GroupInterval = FieldGroupInterval.DateDay });
foreach (var groupField in group)
pivotTable.Fields.Add(groupField);
pivotTable.Groups.Add(group);
group.Tag = field;
}
else
{
var yearField = group[0];
yearField.Area = field.Area;
yearField.AreaIndex = field.AreaIndex;
yearField.ShowInCustomizationForm = true;
}
field.Visible = false;
field.ShowInCustomizationForm = false;
pivotTable.EndUpdate();
};
subMenu.Items.Add(button);
}
foreach (var dateTimeInterval in dateTimeIntervals.Where(item => item != groupInterval))
{
var button = new BarButtonItem() { Content = dateTimeInterval, Tag = field };
subMenu.Items.Add(button);
button.ItemClick += (s, eventArgs) =>
{
pivotTable.BeginUpdate();
var group = field.Group;
if (group != null)
{
var yearField = field;
field = yearField.Tag as PivotGridField;
field.Area = yearField.Area;
field.AreaIndex = yearField.AreaIndex;
field.ShowInCustomizationForm = true;
yearField.Visible = false;
yearField.ShowInCustomizationForm = false;
}
else if (groupInterval != FieldGroupInterval.Default)
field.Caption = field.Caption.Replace(" (" + groupInterval + ")", string.Empty);
field.GroupInterval = dateTimeInterval;
if (dateTimeInterval != FieldGroupInterval.Default)
field.Caption += " (" + dateTimeInterval + ")";
pivotTable.EndUpdate();
};
}
e.Customizations.Add(subMenu);
}
结果如下:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句