Page tree
Skip to end of metadata
Go to start of metadata

在设计器中,如果一个属性的值希望在另一个属性值变化时自动随之变化,可以通过在XXXDesigner.cs 类中重写 OnPropertyEditorChanged 方法来实现。

下面示例中,假设单元格上有两个属性,分别是MyProperty1和MyProperty2,实现的联动效果为 当MyProperty1 的值为 true时,修改 MyProperty2 值为真,否则,修改MyProperty2 值为假。

在 MyPluginCellType.cs 修改代码如下:


using GrapeCity.Forguncy.CellTypes;
using System.ComponentModel;

namespace MyPlugin
{
    [Designer("MyPlugin.Designer.MyPluginCellTypeDesigner, MyPlugin")]
    public class MyPluginCellType : CellType
    {
        public bool MyProperty1 { get; set; }

        public string MyProperty2 { get; set; }
    }
}

在 MyPluginCellTypeDesigner.cs 修改代码如下:

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

namespace MyPlugin.Designer
{
    public class MyPluginCellTypeDesigner : CellTypeDesigner<MyPluginCellType>
    {
        public override void OnPropertyEditorChanged(string propertyName, object propertyValue, Dictionary<string, IEditorSettingsDataContext> properties)
        {
            if (propertyName == nameof(MyPluginCellType.MyProperty1))
            {
                var property2Setting = properties[nameof(MyPluginCellType.MyProperty2)];
                if (object.Equals(propertyValue, true))
                {
                    property2Setting.Value = "真";
                }
                else
                {
                    property2Setting.Value = "假";
                }
            }
            base.OnPropertyEditorChanged(propertyName, propertyValue, properties);
        }
    }
}

代码说明

OnPropertyEditorChanged 函数会在单元格的任何属性被修改时被调用。可以通过参数 propertyName 判断当前被修改的属性名, 通过参数 propertyValue 获取最新被修改的值。properties 属性中包含了全部属性列。可以通过操作 properties 里的特定属性实现联动效果。


设计器中的效果:



  • No labels