2014-02-24 116 views
0

比方說,我有這個類定義中的DevExpress PropertyGrid.SelectedObject的級編輯型(在位編輯器)

public sealed class OptionsGrid 
{ 

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

是否有任何機會,以確定哪些編輯接枝(eG MemoEdit)應該被用於此排在課堂本身?

的Propertygrids SelectedObject設置這樣

propertyGridControl1.SelectedObject = new OptionsGrid(); 

回答

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

編輯:性能得到改善。

+0

非常感謝你的時間,作品非常棒! – Belial09

+0

通過將'e.Row.Properties.Rowdata!= null'移動到方法的頂部,我稍微改進了性能。 – Dmitry

+0

非常感謝你@Dmitry – Belial09