ComboListCell 创建可编辑的组合框单元格:用户可以自由输入、使用自动补全,或从下拉列表中选择。与 DropdownListCell(仅选择)不同,ComboListCell 接受用户输入的新值。
命名空间
using unvell.ReoGrid.CellTypes;
创建组合列表单元格
var sheet = grid.CurrentWorksheet;
var combo = new ComboListCell("Apple", "Banana", "Orange", "Applause", "Appreciate");
sheet.Cells["C3"].Body = combo;
构造函数
| 构造函数 | 说明 |
|---|---|
ComboListCell() | 空的组合列表 |
ComboListCell(params string[] candidates) | 使用字符串项创建 |
ComboListCell(List<string> candidates) | 使用可变列表创建(保持链接) |
ComboListCell(IEnumerable<string> candidates) | 使用任何集合创建 |
ComboListCell(ReferenceRange refRange) | 使用单元格范围作为数据源创建 |
使用可变列表
传入 List<string> 以保持项目同步。添加到列表中的新项目会出现在下拉列表中:
var items = new List<string> { "Alpha", "Beta", "Gamma" };
var combo = new ComboListCell(items);
sheet["B5"] = combo;
items.Add("Delta"); // 立即在下拉列表中可用
使用单元格范围
绑定到范围,使列表反映当前单元格内容:
var combo = new ComboListCell(sheet.Ranges["A2:A20"]);
sheet["D2"] = combo;
属性
ComboListCell 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
SelectedItem | object | — | 当前选中的项 |
SelectedIndex | int | — | 选中项的索引 |
EnableAutoCompletion | bool | true | 输入时启用自动补全 |
AutoCompleteComparerator | Func<string, string, bool> | 前缀匹配 | 自定义比较函数 |
CandidationListSearchProvider | Func<object, IList<string>> | null | 用于大型/远程数据集的自定义搜索提供程序 |
继承自 DropdownCell
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
PullDownOnClick | bool | false | 点击单元格时打开下拉列表 |
DropdownButtonSize | RGSize | 16x16 | 下拉按钮的大小 |
DropdownButtonAutoHeight | bool | true | 自动调整按钮高度以匹配单元格 |
IsDropdown | bool | — | 下拉面板当前是否打开 |
DropdownPanelHeight | int | 200 | 下拉面板的高度 |
MinimumDropdownWidth | int | 40 | 下拉面板的最小宽度 |
MaximumDropdownWidth | int | 1200 | 下拉面板的最大宽度 |
DropdownControl | Control | — | 面板中的平台控件 |
DropdownButtonStyle | DropdownCellStyle | — | 下拉按钮的视觉样式 |
事件
| 事件 | 说明 |
|---|---|
SelectedItemChanged | 选中项改变时触发 |
DropdownOpened | 下拉面板打开时触发(继承) |
DropdownClosed | 下拉面板关闭时触发(继承) |
combo.SelectedItemChanged += (s, e) =>
{
Console.WriteLine($"Selected: {combo.SelectedItem}");
};
方法
| 方法 | 说明 |
|---|---|
PushDown(bool forceCellEdit = true) | 以编程方式打开下拉面板 |
PullUp() | 关闭下拉面板 |
Clone() | 创建此单元格主体的副本 |
自动补全
自动补全默认启用。当用户输入时,匹配的项目会被建议,剩余文本会自动填充。
禁用自动补全
combo.EnableAutoCompletion = false;
自定义比较逻辑
默认比较使用不区分大小写的前缀匹配(StartsWith)。可以覆写以实现不同的行为:
// 子字符串匹配代替前缀匹配
combo.AutoCompleteComparerator = (item, text) =>
{
if (string.IsNullOrWhiteSpace(text)) return false;
return item.Contains(text, StringComparison.OrdinalIgnoreCase);
};
函数签名为 Func<string, string, bool>,其中:
- 第一个参数:候选项文本
- 第二个参数:用户已输入的文本
- 返回
true以将该项包含在建议中
自定义搜索提供程序
对于大型或远程数据集,提供一个动态返回过滤候选项的搜索提供程序:
combo.CandidationListSearchProvider = text =>
{
return ProductSearch(text?.ToString())
.Take(20)
.ToList();
};
函数签名为 Func<object, IList<string>>,其中:
- 参数:用户当前输入的文本
- 返回:要显示的过滤候选列表
设置搜索提供程序后,它会完全替换默认的过滤行为。
捕获新值
由于 ComboListCell 允许自由文本输入,用户可以输入不在原始列表中的值。可以捕获这些值并添加到将来的建议中:
var items = new List<string> { "Alpha", "Beta" };
var combo = new ComboListCell(items);
sheet["B5"] = combo;
sheet.AfterCellEdit += (s, e) =>
{
if (ReferenceEquals(e.Cell.Body, combo) &&
e.NewData is string text &&
!string.IsNullOrWhiteSpace(text) &&
!items.Contains(text))
{
items.Add(text);
}
};
键盘行为
| 按键 | 操作 |
|---|---|
| 上 / 下 | 在建议列表中导航 |
| 回车 / 空格 | 确认高亮的项 |
| Tab | 确认并移动到下一个单元格 |
| Escape | 关闭下拉列表但不选择 |
WPF 样式
在 WPF 上,通过 Style 属性自定义列表外观:
#if WPF
combo.Style.TextColor = SolidColor.DarkBlue;
combo.Style.BackColor = SolidColor.LightYellow;
combo.Style.FontSize = 14;
combo.Style.FontWeight = FontWeights.Bold;
#endif
ComboListStyle 属性(仅 WPF)
| 属性 | 类型 | 说明 |
|---|---|---|
TextColor | SolidColor | 列表中的文本颜色 |
BackColor | SolidColor | 背景色 |
Font | FontFamily | 字体系列 |
FontWeight | FontWeight | 字体粗细 |
FontSize | double | 字体大小 |
FontStyle | FontStyle | 字体样式 |
HorizontalAlignment | HorizontalAlignment | 项目水平对齐 |
VerticalAlignment | VerticalAlignment | 项目垂直对齐 |
Padding | PaddingValue | 项目内边距 |
Reset() | 方法 | 重置为默认样式 |
覆写 ListBox 模板(WPF)
用自定义 ControlTemplate/ItemTemplate 替换下拉列表的 ListBox:
<Window.Resources>
<ControlTemplate x:Key="ReoGridComboListTemplate" TargetType="ListBox">
<Border Background="#101820" CornerRadius="6" Padding="4">
<ScrollViewer Focusable="False">
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
<DataTemplate x:Key="ReoGridComboItemTemplate">
<StackPanel Orientation="Horizontal" Margin="4,2">
<Ellipse Width="6" Height="6" Fill="#FF7A00" Margin="0,0,6,0"/>
<TextBlock Text="{Binding}" Foreground="White"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
在代码中关联:
#if WPF
combo.DropdownOpened += (s, e) =>
{
if (((ComboListCell)s).DropdownControl is ListBox listBox)
{
listBox.Template = (ControlTemplate)FindResource("ReoGridComboListTemplate");
listBox.ItemTemplate = (DataTemplate)FindResource("ReoGridComboItemTemplate");
}
};
#endif
扩展 ComboListCell
通过子类化 ComboListCell 进行更深层次的自定义:
class ProductComboCell : ComboListCell
{
private readonly ProductService _service;
public ProductComboCell(ProductService service) : base()
{
_service = service;
CandidationListSearchProvider = text =>
_service.Search(text?.ToString()).Select(p => p.Name).ToList();
}
}
对比:DropdownListCell 与 ComboListCell
| 功能 | DropdownListCell | ComboListCell |
|---|---|---|
| 从列表选择 | 是 | 是 |
| 自由文本输入 | 否 | 是 |
| 自动补全 | 否 | 是(可配置) |
| 自定义搜索提供程序 | 否 | 是 |
| 接受新值 | 否 | 是 |
类层次结构
CellBody
└─ DropdownCell (面板、按钮、打开/关闭)
└─ DropdownListBaseCell (候选源)
├─ DropdownListCell (仅选择)
└─ ComboListCell (可编辑 + 自动补全)
相关主题
- 内置单元格类型 — 所有单元格类型概览
- 下拉列表单元格 — 仅选择的下拉列表
- 自定义单元格 — 创建自定义单元格主体
- 如何创建自定义下拉单元格 — 自定义下拉教程