0

let's say i have this class

public sealed class OptionsGrid
{

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

is there any chance to define which Edit (e.G. MemoEdit) should be used for this row in the class itself?

The Propertygrids SelectedObject is set like this

propertyGridControl1.SelectedObject = new OptionsGrid();
Belial09
  • 694
  • 1
  • 11
  • 24

1 Answers1

3

You can define your own attribute containing a type of the desired editor:

[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; }
}

Then you should set it in the PropertyGrid.CustomDrawRowValueCell as follows:

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);
        }
    }
}

See also (scroll to the bottom): https://documentation.devexpress.com/#WindowsForms/CustomDocument429

EDIT: Perfomance improved.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
  • I slightly improved performance by moving `e.Row.Properties.Rowdata != null` to the top of the method. – Dmitry Feb 24 '14 at 17:48
  • Is there a way to add click event to this attribute? (not in propertyGrid_CustomDrawRowValueCell) – Muhammed Tanriverdi Nov 06 '14 at 10:41
  • No. You can only set the event handler to the attribute's property at runtime, not at the design-time, therefore this attribute would be applicable to the classes and structs only, but not to their members. – Dmitry Nov 06 '14 at 10:57
  • Thanks for your response. Do you know or have any recommendation about how visual studio achieve this. Because there a lot of buttons in propertygrid. I think they don't implement "click events" in "propertyGrid_CustomDrawRowValueCell". Because there is only one propertygrid and a lot of objects related to this propertygrid. – Muhammed Tanriverdi Nov 06 '14 at 20:09
  • I found what i want. Thanks. As you said it should be implemented in class level. There is a link about this topic http://stackoverflow.com/questions/1016239/how-to-create-custom-propertygrid-editor-item-which-opens-a-form – Muhammed Tanriverdi Nov 07 '14 at 08:48