Skip to content

列表属性

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

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

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

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

在设计器中效果如下

1669532538249-1715c27f-0a7f-4442-9211-8646ab190fdc.png

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

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

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

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

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

        [CustomCommandObject]
        [DisplayName("点击命令")]
        public object ClickCommand { get; set; }
    }

在设计器中效果如下

1669532657141-f27b561b-97d6-497f-8cf9-6e320a88c69c.png

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

  1. 控制列表最大或最小元素个数
    1. 设置ListPropertyAttribute 的 MaxCount 和 MinCount 属性
    2. 代码
csharp
    public class MyPluginCellType : CellType
    {
        [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 MyPluginCellType : CellType
    {
        [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 MyPluginCellType : CellType
    {
        [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 MyPluginCellType : CellType
    {
        [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 MyPluginCellType : CellType
    {
        [FlatListProperty]
        public List<MyObj> MyProperty { get; set; }
    }

    public class MyObj : ObjectPropertyBase
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
3. 设计器时效果![1707218044960-39bfe2b8-490c-4826-a7a1-0582c00d8eaa.png](./img/qKfREugP8F74YnI5/1707218044960-39bfe2b8-490c-4826-a7a1-0582c00d8eaa-284195.png)
4. 此特性为10.0.0.0版本新增

更新: 2024-02-06 19:14:26
原文: https://www.yuque.com/robert-bh51n/ea8l6c/runvkl9qysvo260u