关于 syncfusion-wpf-excel-like-grid
面向 WPF 开发者的 Syncfusion GridControl 实现指南,介绍表格配置、交互与数据处理。涵盖公式、虚拟化及 Excel 导入导出等表格功能。
主要功能
- 01配置单元格类型与样式
- 02管理行列及选择模式
- 03公式计算与自定义函数
- 04剪贴板及撤销重做
- 05Excel 导入与导出
- 06虚拟数据加载与性能优化
适用场景
- 01在 WPF 应用中添加表格控件
- 02按需加载大型表格数据
- 03实现公式单元格和数据编辑
- 04导入或导出 Excel 工作簿
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ syncfusion/ wpf-ui-components-skills --skill syncfusion-wpf-excel-like-grid安装后在 Claude Code 中输入/syncfusion-wpf-excel-like-grid确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/advanced-features.mdappearance-and-styling.mdcell-types.mdediting-clipboard-undo.mdexcel-import-export.mdformula-cells.mdgetting-started.mdinteractive-features.mdmanaging-rows-and-columns.mdperformance-virtualization.mdselection-and-events.md详细介绍
实现 Syncfusion WPF GridControl
Syncfusion WPF GridControl 是一款高性能、以单元格为核心的网格,可用作灵活的表格显示引擎。它不对数据结构做任何假设,支持通过 QueryCellInfo 按需虚拟加载数据,并提供类似 Excel 的行为,包括公式、导入/导出、剪贴板操作、合并单元格以及 20 多种内置单元格类型。
何时使用此技能
- 在 WPF 应用程序中添加 GridControl(使用 XAML 或代码隐藏)
- 通过直接赋值或虚拟
QueryCellInfo事件填充单元格 - 配置单元格类型(CheckBox、ComboBox、DateTimeEdit、FormulaCell 等)
- 自定义单元格外观(背景、字体、边框、数据格式)
- 管理行和列(插入、删除、移动、冻结、隐藏)
- 剪贴板、撤销/重做和编辑行为
- 交互功能:拖放列、拖放单元格、调整大小
- 公式单元格支持和内置公式库
- 将网格数据导出到 Excel;将 Excel 工作簿导入网格
- 选择模式、事件和虚拟模式模式
- 性能调优、虚拟化、合并范围、缩放
- 高级功能:批注、工具提示、打印、序列化、自动调整大小
文档和导航指南
入门
📄 阅读: references/getting-started.md
- 必需的程序集引用
- 通过设计器或以编程方式添加 GridControl
- 设置
RowCount和ColumnCount - 通过循环或
QueryCellInfo虚拟事件填充数据 - 最小可运行示例
单元格类型
📄 阅读: references/cell-types.md
- 20 多种内置单元格类型:Header、Static、CheckBox、Button、Image、ComboBox、 DropDownList、DateTimeEdit、IntegerEdit、DoubleEdit、CurrencyEdit、PercentEdit、 MaskEdit、UpDownEdit、RichText、DataBoundTemplate、NestedGrid(ScrollGrid)
- 在单个单元格或范围上设置
Style.CellType - 使用
ChoiceList与ItemsSource的 ComboBox - 自定义单元格类型(CellModel + CellRenderer 模式)
外观和样式
📄 阅读: references/appearance-and-styling.md
GridStyleInfo类和样式继承层次结构- 易变单元格样式与渲染单元格样式
- 基础样式(
BaseStylesMap)、TableStyle、RowStyles、ColumnStyles - 背景(纯色/渐变)、前景、字体、文本方向、边框
- 数值和 DateTime 格式字符串;
FormatProvider接口
管理行和列
📄 阅读: references/managing-rows-and-columns.md
- 设置行/列数和默认大小
- 隐藏和取消隐藏行/列(
SetHidden) - 冻结行/列;页脚和标题行/列
- 在运行时插入、移动和删除行/列
- 调整大小行为和禁用调整大小控制器
编辑、剪贴板和撤销/重做
📄 阅读: references/editing-clipboard-undo.md
CopyPasteOption标志(CopyText、CopyCellData、PasteCell、CutCell、XmlCopyPaste)- 使用
TextDataExchange自定义分隔符并执行基于缓冲区的操作 - 剪贴板事件和
IGridCopyPaste自定义实现 CommandStack:撤销/重做、BeginTrans、CommitTrans、Rollback- 创建派生命令以扩展撤销/重做功能
交互功能
📄 阅读: references/interactive-features.md
- 列拖放(
AllowDragColumns) - 类似 Excel 的单元格拖放(
AllowDragDrop、DataObjectConsumerOptions) - 在运行时调整行/列大小以及禁用调整大小控制器
- 通过
HiddenBorderBrush和SetHidden直观地隐藏/取消隐藏行和列
公式单元格
📄 阅读: references/formula-cells.md
- 为单个单元格或整个网格启用
FormulaCell类型 - 内置公式库(150+ 个函数:Sum、Avg、Sqrt、Pow、Cos、Sin 等)
- 算术运算符和计算优先级
- 跨单元格引用(A1 表示法)、命名区域
- 向库中添加自定义公式函数
Excel 导入和导出
📄 阅读: references/excel-import-export.md
- 所需程序集(
Syncfusion.XlsIO、Syncfusion.GridConverter.Wpf) - 将整个网格或选定内容导出为 .xls / .xlsx
- 导入 Excel 工作簿:样式、公式、条件格式、冻结窗格、 超链接、批注
- 针对大型工作簿的虚拟化导入
选择和事件
📄 阅读: references/selection-and-events.md
- 选择模式:单元格、行、列(
AllowSelection标志) - 用于虚拟数据加载的
QueryCellInfo事件模式 - 用于针对视图进行覆盖的
PrepareRenderCell - 常见网格事件:
CommitCellInfo、CurrentCellActivated、剪贴板事件、RowsInserted、ColumnsMoved、ResizingRows等 - 使用
GridRangeInfo和SelectedRanges
性能和虚拟化
📄 阅读: references/performance-virtualization.md
- 虚拟模式基础知识和
QueryCellInfo最佳实践 - 覆盖区域(
CoveredCells)和浮动单元格 - 缩放网格
- 高频更新模式
高级功能
📄 阅读: references/advanced-features.md
- 单元格批注、输入消息提示和工具提示
- 单元格布局自定义(覆盖单元格、合并区域、自动调整)
- 打印网格内容
- 序列化:保存和加载网格状态
- 根据内容自动调整行高和列宽
快速入门示例
1. Add assemblies:
Syncfusion.Grid.Wpf
Syncfusion.GridCommon.Wpf,
Syncfusion.Linq.Base,
Syncfusion.Shared.Wpf
// 2. XAML — place GridControl inside a ScrollViewer
<ScrollViewer>
<syncfusion:GridControl x:Name="gridControl" />
</ScrollViewer>
// 3. Code-behind: set size and populate
gridControl.Model.RowCount = 100;
gridControl.Model.ColumnCount = 10;
// Option A — direct assignment
for (int i = 0; i < 100; i++)
for (int j = 0; j < 10; j++)
gridControl.Model[i, j].CellValue = $"{i}/{j}";
// Option B — virtual mode (preferred for large data)
gridControl.QueryCellInfo += (s, e) =>
{
if (e.Cell.RowIndex > 0 && e.Cell.ColumnIndex > 0)
e.Style.CellValue = $"{e.Cell.RowIndex}/{e.Cell.ColumnIndex}";
};
常见模式
将单元格类型应用于区域
// Set an entire column to CheckBox
for (int i = 1; i <= gridControl.Model.RowCount; i++)
gridControl.Model[i, 3].CellType = "CheckBox";
通过 PrepareRenderCell 设置交替行颜色
gridControl.PrepareRenderCell += (s, e) =>
{
if (e.Cell.RowIndex > 0 && e.Cell.RowIndex % 2 == 0)
e.Style.Background = Brushes.LightSkyBlue;
};
冻结标题行并启用公式单元格
gridControl.Model.FrozenRows = 1;
gridControl.Model.HeaderRows = 1;
gridControl.BaseStylesMap["Standard"].StyleInfo.CellType = "FormulaCell";
导出到 Excel
gridControl.Model.ExportToExcel(@"Output.xlsx", ExcelVersion.Excel2007);
关键样式属性
| 属性 | 用途 |
|---|---|
Model[r, c].CellType | 设置内置或自定义单元格类型字符串 |
Model[r, c].CellValue | 存储在单元格中的数据值 |
Model[r, c].Background | 单元格背景画刷 |
Model[r, c].Foreground | 单元格文本颜色 |
Model[r, c].Font.FontSize | 单元格字体大小 |
Model[r, c].Borders | 单独的边框画笔(Top/Bottom/Left/Right) |
Model[r, c].Format | 格式字符串(例如 "C"、"0.00"、"d") |
Model.RowHeights[r] | 以设备无关单位表示的行高 |
Model.ColumnWidths[c] | 列宽 |
Model.FrozenRows | 顶部冻结的行数 |
Model.FrozenColumns | 左侧冻结的列数 |