界面组件DevExpress WPF中文教程:Grid - 如何创建栏(Bands)?
DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。
本文将为大家介绍如何使用DevExpress WPF GridControl创建栏(Bands)?欢迎下载最新版组件体验!
DevExpress技术交流群11:749942875 欢迎一起进群讨论
DevExpress WPF GridControl的TableView和TreeListView允许你将列组织成逻辑组,这些组被称为栏(bands),每个栏(bands)由栏(bands)标题和子列组成,栏(bands)标题显示在栏(bands)子列上方的栏(bands)面板中。

创建栏(Bands)
栏(Bands)是GridControlBand对象,GridControl将其存储在GridControl.Bands集合中。

在设计时
您可以使用GridControl的Quick Actions菜单来添加栏(Bands)。

DevExpress WPF GridControlBand的的Quick Actions菜单允许您添加子栏(Bands)和列,并指定栏(Bands)的Header属性:

在XAML中
1. 将GridControlBand对象添加到GridControl.Bands集合。
2. 使用栏(Bands)的Header属性指定栏(Bands)中显示的文本。
3. 用GridColumn对象填充栏(Bands)的Columns集合。
4. 指定列设置。
XAML
<dxg:GridControl ItemsSource="{Binding Source}"> <dxg:GridControl.Bands> <dxg:GridControlBand Header="Product"> <dxg:GridColumn FieldName="ProductName"/> </dxg:GridControlBand> <dxg:GridControlBand Header="Order Info"> <dxg:GridColumn FieldName="Country"/> <dxg:GridColumn FieldName="City"/> <dxg:GridColumn FieldName="OrderDate"/> </dxg:GridControlBand> <dxg:GridControlBand Header="Pricing"> <dxg:GridColumn FieldName="UnitPrice"/> <dxg:GridColumn FieldName="Quantity"/> </dxg:GridControlBand> </dxg:GridControl.Bands> </dxg:GridControl>
在代码中
C#
// Create band objects and specify their settings: var productBand = new GridControlBand(); productBand.Header = "Product"; productBand.Columns.Add(new GridColumn() { FieldName = nameof(Product.ProductName) }); var orderBand = new GridControlBand(); orderBand.Header = "Order Info"; orderBand.Columns.Add(new GridColumn() { FieldName = nameof(Product.Country) }); orderBand.Columns.Add(new GridColumn() { FieldName = nameof(Product.City) }); orderBand.Columns.Add(new GridColumn() { FieldName = nameof(Product.OrderDate) }); var pricingBand = new GridControlBand(); pricingBand.Header = "Pricing"; pricingBand.Columns.Add(new GridColumn() { FieldName = nameof(Product.UnitPrice) }); pricingBand.Columns.Add(new GridColumn() { FieldName = nameof(Product.Quantity) }); // Add bands to the GridControl: grid.Bands.Add(productBand); grid.Bands.Add(orderBand); grid.Bands.Add(pricingBand);
VB.NET
Dim productBand = New GridControlBand() productBand.Header = "Product" productBand.Columns.Add(New GridColumn() With { .FieldName = NameOf(Product.ProductName) }) Dim orderBand = New GridControlBand() orderBand.Header = "Order Info" orderBand.Columns.Add(New GridColumn() With { .FieldName = NameOf(Product.Country) }) orderBand.Columns.Add(New GridColumn() With { .FieldName = NameOf(Product.City) }) orderBand.Columns.Add(New GridColumn() With { .FieldName = NameOf(Product.OrderDate) }) Dim pricingBand = New GridControlBand() pricingBand.Header = "Pricing" pricingBand.Columns.Add(New GridColumn() With { .FieldName = NameOf(Product.UnitPrice) }) pricingBand.Columns.Add(New GridColumn() With { .FieldName = NameOf(Product.Quantity) }) grid.Bands.Add(productBand) grid.Bands.Add(orderBand) grid.Bands.Add(pricingBand)
使用数据注释属性
您可以使用数据注释属性将网格列分组为栏(Bands):
1. 将DisplayAttribute应用于数据源中的所有字段。
2. 使用DisplayAttribute.GroupName属性来指定栏(Bands)的标题。
3. 将DataControlBase.EnableSmartColumnsGeneration属性设置为true,来根据数据注释属性生成列。
XAML
<dxg:GridControl ItemsSource="{Binding Source}" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True"> <!-- ... --> </dxg:GridControl>
C#
using System.ComponentModel.DataAnnotations; // ... public class Product { [Display(GroupName = "Product")] public string ProductName { get; set; } [Display(GroupName = "Order Info")] public string Country { get; set; } [Display(GroupName = "Order Info")] public string City { get; set; } [Display(GroupName = "Pricing")] public double UnitPrice { get; set; } [Display(GroupName = "Pricing")] public int Quantity { get; set; } [Display(GroupName = "Order Info")] public DateTime OrderDate { get; set; } }
VB.NET
Imports System.ComponentModel.DataAnnotations ' ... Public Class Product <Display(GroupName:="Product")> Public Property ProductName As String <Display(GroupName:="Order Info")> Public Property Country As String <Display(GroupName:="Order Info")> Public Property City As String <Display(GroupName:="Pricing")> Public Property UnitPrice As Double <Display(GroupName:="Pricing")> Public Property Quantity As Integer <Display(GroupName:="Order Info")> Public Property OrderDate As DateTime End Class
绑定到没有GroupName属性的字段的列显示在第一个栏(Bands)中。