2015-05-12 66 views

回答

4

一種解決方案是申報TypeConverter,做...什麼都沒有,這樣的事情:

這是你要編輯的類:

public class MyClass 
{ 
    [Editor(typeof(MyClassEditor), typeof(UITypeEditor))] 
    [TypeConverter(typeof(MyConverter))] 
    public string MyProperty { get; set; } 
} 

這是自定義UITypeEditor的:

public class MyClassEditor : UITypeEditor 
{ 
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
    { 
     return UITypeEditorEditStyle.Modal; 
    } 

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) 
    { 
     MessageBox.Show("press ok to continue"); 
     return "You can't edit this"; 
    } 
} 

這就是著名的轉換器,把我天寫:

// this class does nothing on purpose 
public class MyConverter : TypeConverter 
{ 
} 
+0

感謝您的回覆。這可能是我需要的。我會檢查這一點,如果我需要的話,我會將其標記爲正確的答案。 – dmcnally

0

我發現像這樣(PropertyValueChanged事件PropertyGrid的)一種解決方法:

private void propertyGridNewBonus_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) 
     { 
      switch (e.ChangedItem.Label) 
      { 
       case "somePropertyLabel": 
        newBonus.somePropertyValue = e.OldValue.ToString(); 
        break; 
       default: 
        break; 
      } 
     } 

只是恢復舊值,而用戶在PropertyGrid中編輯它。 這看起來像值是可編輯的,但在合併舊值後恢復,因此改變它的唯一方法是使用自定義的TypeConverter,UITypeEditor等。