2012-07-09 131 views
2

我已經創建了一個具有自定義數組值的屬性網格。當用戶選擇其中一個下拉列表時,我希望它顯示一個表單。我的問題不是它不起作用,它不是活動過度,並且儘管只聲明瞭一次,但顯示了大約6次的形式。如果我選擇ShowDialog,它將顯示兩次窗體,並在嘗試關閉第二個對話框時會創建另外兩個窗體實例。以下是我正在使用的代碼。我無法弄清楚什麼是錯的。C#屬性多次顯示錶單的屬性選擇

//Property Grid Type 
internal class TransferConnectionPropertyConverter : StringConverter 
    { 
     public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
     { 
      return true; 
     } 

     public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) 
     { 
      return true; 
     } 

     public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
     { 
      return new StandardValuesCollection(new string[] { "", NEW_CONN }); 
     }    
    } 

//Property Grid Node 
[Category("Connection"), 
Description("Specifies the type of connection for this task.")] 
[TypeConverter(typeof(TransferConnectionPropertyConverter))] 
public string TransferProtocol 
{ 
    get 
    { 
     if (stConnectionType == NEW_CONN) 
     { 
       ConnectionDetailsForm connDetails = new ConnectionDetailsForm(); 
       connDetails.Show();       
     } 
     return stConnectionType; 
    } 
    set 
    { 
     stConnectionType = value; 
    }          
} 

回答

1

你需要爲一個編輯器,你肯定不希望呈現出財產get property期間的形式,因爲這可能PropertyGrid中的生命週期中被調用多次。

簡單類(從this example找到):

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

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { 
    IWindowsFormsEditorService svc = (IWindowsFormsEditorService) 
     provider.GetService(typeof(IWindowsFormsEditorService)); 
    if (svc != null) { 
     svc.ShowDialog(new ConnectionDetailsForm()); 
     // update etc 
    } 
    return value; 
    } 
} 

然後你裝飾你的財產這個編輯器(和記,我刪除了轉換器,因爲你的財產只是一個字符串,沒什麼可轉換):

[Category("Connection")] 
[Description("Specifies the type of connection for this task.")] 
[Editor(typeof(StringEditor), typeof(UITypeEditor))] 
public string TransferProtocol { 
    get { 
    return stConnectionType; 
    } 
    set { 
    stConnectionType = value; 
    } 
} 
+0

你說得對。我在錯誤的地方打電話給表單表示,把它放在獲得是它多次顯示的原因。 '哦' - 只是沒有想到。我改變了它,並將其放置在按我預期工作的集合中。 – zeencat 2012-07-09 13:23:54