2013-12-20 21 views
1

我有一個綁定到一個List<string> ToolStripComboBox控件。我想在初始化後將可見文本設置爲String.Empty設置空「啓動文本」的ToolStripComboBox控件與數據源

問題是,無論我做什麼,初始化控件後的文本始終是我的清單(預期的內容,但我無法清除此預選文本)的第一個條目

這是我的相關代碼:

public frmPricelist(Pricelist pricelist) 
    { 
     _pricelist = pricelist; 

     InitializeComponent(); 
     Init(); 
    } 

    private void Init() 
    { 
     cmbHersteller.Items.Clear(); 
     cmbHersteller.ComboBox.DataSource = _pricelist.GetHersteller(); 

     Application.DoEvents(); // Inserted for testing purposes 

     cmbHersteller.ComboBox.SelectedText = String.Empty; // does not change the value 
     cmbHersteller.ComboBox.Text   = String.Empty; // does not change the value    
    } 

也許我錯過了捨本逐末,但我根本就沒有得到它的工作:)。

回答

1

在我看來,最好的方法是實際添加一個空的物品。考慮以下內容:

private void Init() 
{ 
    cmbHersteller.Items.Clear(); 

    var list = _pricelist.GetHersteller(); 
    list.Insert(0, ""); 
    cmbHersteller.ComboBox.DataSource = list; 

    cmbHersteller.ComboBox.SelectedIndex = 0; 
} 
+0

我也想到了這一點。但如果可能的話,我想避免這種情況。 – user1567896

+1

@ user1567896,問題是數據綁定組合框直接使用數據綁定項。有一些非常討厭的工作,我不確定哪怕使用組合框的工具條版本。這是最準確和最合適的方式。 –

+1

恐怕你是對的。我試過一些東西,但這個小小的控制確實非常討厭。如果不需要過濾,我將使用文本虛擬如* <所有生產者> *。感謝您的回答! – user1567896