Skip to content

下拉列表属性

如果属性的类型是字符串,默认属性值是可以接受任意字符串的,如果希望提供字符串值候选列表,可以通过标注ComboPropertyAttribute 的并设置ValueList属性的方式实现。多值个值用“|”分隔。
注意,标注ComboPropertyAttribute的属性类型必须是 string

csharp
    public class MyPluginCellType : CellType
    {
        [ComboProperty(ValueList = "Student|Teacher|Worker")]
        public string MyProperty { get; set; }
    }

在设计器中效果如下

1669464854447-c50f791d-f7e2-4fcb-9654-0f7db35402f6.png

如果需要更细致的控制,可以通过ComboPropertyAttribute的其他属性来控制

  1. 值与显示值不同
    1. 设置ComboPropertyAttribute 的 DisplayList 属性
    2. 代码
csharp
    public class MyPluginCellType : CellType
    {
        [ComboProperty(ValueList = "Student|Teacher|Worker", DisplayList ="学生|教师|工人")]
        public string MyProperty { get; set; }
    }
3. 效果  

1669465103436-2c1ab596-cc75-4e10-9edd-cfb4d7f9694b.png 4. 其他说明
此方法可以使用户在选择时选择中文选项,而单元格实际保存值为英文,方便程序处理
ValueList和DisplayList通过数量和顺序匹配。
如果DisplayList数量超出ValueList数量,多出部分会被忽略
如果DisplayList数量少于ValueList数量,不足部分会使用ValueList对应的值 2. 允许用户使用列表以外的值 1. 设置ComboPropertyAttribute 的 IsSelectOnly属性 2. 代码

csharp
    public class MyPluginCellType : CellType
    {
        [ComboProperty(ValueList = "Student|Teacher|Worker", IsSelectOnly = false)]
        public string MyProperty { get; set; }
    }
3. 效果  

1669465564833-d9b27c9b-b19d-4c89-b9c3-b01319e25600.png 4. 注意
IsSelectOnly 为 False 时,DisplayList 设置会被忽略
不填时 IsSelectOnly 属性的默认值为 True 3. 支持搜索 1. 设置ComboPropertyAttribute 的 Searchable 属性 2. 代码

csharp
    public class MyPluginCellType : CellType
    {
        [ComboProperty(ValueList = "aa|bb|cc", Searchable = true)]
        public string MyProperty { get; set; }

        [ComboProperty(ValueList = "aa|bb|cc", Searchable = true, IsSelectOnly = false)]
        public string MyProperty2 { get; set; }
    }
3. 效果  

1693548809718-49153665-61bb-4198-89f7-17ea60ef4993.png
1693548843257-3bcd2a66-51ce-41bf-b4d6-1d6dd1901374.png 4. 策略 1. 如果IsSelectOnly为True则搜索框会在下拉框中 2. 如果IsSelectOnly为False则可以直接输入,下拉框会自动按照输入的字符匹配。此模式下同样可以输入下拉框中不存在的字符串 3. 本特性要求活字格版本大于等于9.0.100.0

动态下拉列表

有时,下拉列表中的选项不是开发时决定的,而是动态生成,例如下拉打印机列表。可以通过重写CellType的Designer 通过代码动态生成列表

javascript
    [Designer("MyPlugin.Designer.MyPluginCellTypeDesigner, MyPlugin")]
    public class MyPluginCellType : CellType
    {
        public string MyProperty { get; set; }
    }

    public class MyPluginCellTypeDesigner : CellTypeDesigner<MyPluginCellType>
    {
        public override EditorSetting GetEditorSetting(PropertyDescriptor property, IBuilderContext builderContext)
        {
            if (property.Name == nameof(MyPluginCellType.MyProperty))
            {
                var list = new string[] { "aaa", "bbb", "ccc" };
                return new ComboEditorSetting(list);
            }
            return base.GetEditorSetting(property, builderContext);
        }
    }

如果希望下拉列表的显示值和选择后保存的值不一样,可以如下修改 GetEditorSetting 方法,让List的每一项不是字符串,而是一个对象。通过设置 ComboEditorSetting 的 displayMember和valueMember来指定对象的哪个属性用于显示,哪个属性用于保存值

csharp
    public class MyPluginCellTypeDesigner : CellTypeDesigner<MyPluginCellType>
    {
        public override EditorSetting GetEditorSetting(PropertyDescriptor property, IBuilderContext builderContext)
        {
            if (property.Name == nameof(MyPluginCellType.MyProperty))
            {
                var list = new List<ComboItem>
                {
                    new ComboItem(null, "<空>"),
                    new ComboItem("student", "学生"),
                    new ComboItem("teacher", "教师")
                };
                return new ComboEditorSetting(list, nameof(ComboItem.Display), nameof(ComboItem.Value));
            }
            return base.GetEditorSetting(property, builderContext);
        }
    }
    public class ComboItem
    {
        public ComboItem(string value, string display)
        {
            Value = value;
            Display = display;
        }
        public string Value { get; set; }
        public string Display { get; set; }
    }

更新: 2024-07-10 18:10:42
原文: https://www.yuque.com/robert-bh51n/ea8l6c/ldesl7xgrafdav8k