Skip to content

列表属性

如果一个属性的类型是List类型,List的每一项又包含了子属性,那么可以通过标注ListPropertyAttribute,使得活字格设计器可以通过弹出二级对话框来编辑该属性。

注意,ObjType属性里声明的类型必须与属性类型一致
自定义对象的类型应该从 ObjectPropertyBase 类派生,以确保在单元格复制的时候,子属性可以被正确的深克隆(ObjectPropertyBase实现了默认的深克隆逻辑)

csharp
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System.Collections.Generic;

namespace MyPlugin
{
    public class MyPluginCommand : Command
    {
        [ListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

在设计器中效果如下

1669650207903-d1f96004-599c-4960-8d8b-4d435ab9a515.png

列表项目的子属性也可以通过标注来控制属性编辑控件

下面例子中,额外声明了两个属性分别使用公式编辑器和命令编辑器。具体标注的用法请参考之前的章节

csharp
    public class MyPluginCommand : Command
    {
        [ListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        public string Name { get; set; }

        [FormulaProperty]
        public object FormulaProperty { get; set; }

        [ComboProperty(ValueList = "选项1|选项2|选项3")]
        public string Type { get; set; }
    }

在设计器中效果如下

1669650530391-1b9ad0eb-d055-4f5b-bd4d-45ac54abe1f5.png

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

  1. 控制列表最大或最小元素个数
    1. 设置ListPropertyAttribute 的 MaxCount 和 MinCount 属性
    2. 代码
csharp
    public class MyPluginCommand : Command
    {
        [ListProperty(MaxCount = 5, MinCount = 1)]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj: ObjectPropertyBase
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
  1. 指定属性的默认值
    1. 给属性添加ListPropertyItemSettingAttribute 的 DefaultName 属性
    2. 代码
csharp
    public class MyPluginCommand : Command
    {
        [ListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        [ListPropertyItemSetting(DefaultName = "Node")]
        public string Name { get; set; }
        public string Description { get; set; }
    }
3. **设计器时效果**  

1707118777871-2d1117a3-e99f-47c7-90ac-206dd44ce468.gif 4. 此特性为10.0.0.0版本新增 3. 指定属性的默认列宽 1. 给属性添加ListPropertyItemSettingAttribute 的 DefaultWidth 属性(列表属性的默认列宽为150个像素) 2. 代码

csharp
    public class MyPluginCommand : Command
    {
        [ListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        [ListPropertyItemSetting(DefaultWidth = 250)]
        public string Name { get; set; }
        public string Description { get; set; }
    }
3. 设计器时效果  

1707119074405-e075f82c-f35c-401c-8d6c-14eb6dddc0fa.png 4. 此特性为10.0.0.0版本新增 4. 指定属性值不可重复 1. 给属性添加ListPropertyItemSettingAttribute 的 IsUnique 属性 2. 代码

csharp
    public class MyPluginCommand : Command
    {
        [ListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        [ListPropertyItemSetting(IsUnique = true)]
        public string Name { get; set; }
        public string Description { get; set; }
    }
3. 设计器时效果  

1707119608666-69f28a18-10a5-404f-8477-ea579b1849ce.png 4. 此特性为10.0.0.0版本新增 5. 内嵌显示 1. 通过标注 FlatListPropertyAttribute 可以让列表在属性面板中内嵌显示 2. 代码

csharp
    public class MyPluginCommand : Command
    {
        [FlatListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
3. 设计器时效果![1707218193255-f6a40eae-46a1-4838-a107-ea242e69c542.png](./img/UAF21MRVDg-Luyon/1707218193255-f6a40eae-46a1-4838-a107-ea242e69c542-955623.png)
4. 此特性为10.0.0.0版本新增

更新: 2024-02-06 19:16:52
原文: https://www.yuque.com/robert-bh51n/ea8l6c/tdu8rxygvs430pdt