0

假设我有这门课

public sealed class OptionsGrid
{

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

有没有机会在类本身中定义该行应该使用哪个编辑(eG MemoEdit)?

Propertygrids SelectedObject 是这样设置的

propertyGridControl1.SelectedObject = new OptionsGrid();
4

1 回答 1

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

编辑:性能提高。

于 2014-02-24T16:52:01.460 回答