在Devexpress PropertyGrid.SelectedObject的类中定义Edit-Type(就地编辑器)

时间:2014-02-24 15:26:09

标签: c# winforms devexpress propertygrid system.componentmodel

让我说我有这个班级

public sealed class OptionsGrid
{

   [Description("Teststring"), DisplayName("DisplaynameTest"), Category("Test")]
   public string Test { get; set; }
}

有没有机会定义哪个Edit(例如MemoEdit)应该用于类本身的这一行?

Propertygrids SelectedObject设置如下

propertyGridControl1.SelectedObject = new OptionsGrid();

1 个答案:

答案 0 :(得分:3)

您可以定义自己的属性,其中包含所需编辑器的类型:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class EditorControlAttribute : Attribute
{
    private readonly Type type;

    public Type EditorType
    {
        get { return type; }
    }

    public EditorControlAttribute(Type type)
    {
        this.type = type;
    }
}

public sealed class OptionsGrid
{
    [Description("Teststring"), DisplayName("DisplaynameTest"), Category("Test")]
    [EditorControl(typeof(RepositoryItemMemoEdit))]
    public string Test { get; set; }
}

然后你应该在PropertyGrid.CustomDrawRowValueCell中设置如下:

private void propertyGrid_CustomDrawRowValueCell(object sender, DevExpress.XtraVerticalGrid.Events.CustomDrawRowValueCellEventArgs e)
{
    if (propertyGrid.SelectedObject == null || e.Row.Properties.RowEdit != null)
        return;

    System.Reflection.MemberInfo[] mi = (propertyGrid.SelectedObject.GetType()).GetMember(e.Row.Properties.FieldName);
    if (mi.Length == 1)
    {
        EditorControlAttribute attr = (EditorControlAttribute)Attribute.GetCustomAttribute(mi[0], typeof(EditorControlAttribute));
        if (attr != null)
        {
            e.Row.Properties.RowEdit = (DevExpress.XtraEditors.Repository.RepositoryItem)Activator.CreateInstance(attr.EditorType);
        }
    }
}

另请参阅(滚动到底部):https://documentation.devexpress.com/#WindowsForms/CustomDocument429

编辑:性能提升。