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 属性

属性类型默认值说明
SelectedItemobject当前选中的项
SelectedIndexint选中项的索引
EnableAutoCompletionbooltrue输入时启用自动补全
AutoCompleteCompareratorFunc<string, string, bool>前缀匹配自定义比较函数
CandidationListSearchProviderFunc<object, IList<string>>null用于大型/远程数据集的自定义搜索提供程序

继承自 DropdownCell

属性类型默认值说明
PullDownOnClickboolfalse点击单元格时打开下拉列表
DropdownButtonSizeRGSize16x16下拉按钮的大小
DropdownButtonAutoHeightbooltrue自动调整按钮高度以匹配单元格
IsDropdownbool下拉面板当前是否打开
DropdownPanelHeightint200下拉面板的高度
MinimumDropdownWidthint40下拉面板的最小宽度
MaximumDropdownWidthint1200下拉面板的最大宽度
DropdownControlControl面板中的平台控件
DropdownButtonStyleDropdownCellStyle下拉按钮的视觉样式

事件

事件说明
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)

属性类型说明
TextColorSolidColor列表中的文本颜色
BackColorSolidColor背景色
FontFontFamily字体系列
FontWeightFontWeight字体粗细
FontSizedouble字体大小
FontStyleFontStyle字体样式
HorizontalAlignmentHorizontalAlignment项目水平对齐
VerticalAlignmentVerticalAlignment项目垂直对齐
PaddingPaddingValue项目内边距
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

功能DropdownListCellComboListCell
从列表选择
自由文本输入
自动补全是(可配置)
自定义搜索提供程序
接受新值

类层次结构

CellBody
  └─ DropdownCell (面板、按钮、打开/关闭)
       └─ DropdownListBaseCell (候选源)
            ├─ DropdownListCell (仅选择)
            └─ ComboListCell (可编辑 + 自动补全)

相关主题

这篇文章对您有帮助吗?