[]
        
(Showing Draft Content)

创建单元格时初始化属性值

如果单元格的属性只有在新建的时候需要初始化部分属性值(通常用于初始列表、对象、树等复杂结构),可以通过让CellTypeDesigner实现ISupportPropertyInitialize方法。

假设MyPluginCellType 有一个Items属性是一个列表,代码如下:

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

namespace MyPlugin
{
    [Icon("pack://application:,,,/MyPlugin;component/Resources/Icon.png")]
    [Designer("MyPlugin.Designer.MyPluginCellTypeDesigner, MyPlugin")]
    public class MyPluginCellType : CellType
    {
        [ListProperty]
        public List<Item> Items { get; set; } = new List<Item>();
    }
    public class Item : ObjectPropertyBase
    {
        public string Name { get; set; }
    }
}

在MyPluginCellTypeDesigner,可以通过实现ISupportPropertyInitialize来初始化 Items属性。

    public class MyPluginCellTypeDesigner : CellTypeDesigner<MyPluginCellType>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new MyPluginCellTypeDrawingControl(this.CellType, cellInfo, drawingHelper);
        }
        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            this.CellType.Items.Add(new Item() { Name = "项目1" });
            this.CellType.Items.Add(new Item() { Name = "项目2" });
            this.CellType.Items.Add(new Item() { Name = "项目3" });
        }
    }