2012-02-22 49 views
0

您好我想添加文本框到列表框中,每次點擊添加按鈕。 所以這裏是我的代碼無法添加文本框到列表框

this.ListBox1.Controls.Add(TB);//TB is the name of my TextBox and it has been defined and initialized 

我甚至試過用Addat

沒有錯誤,但它不工作

+0

如果您試圖使列表框可編輯,您需要爲此編寫自定義控件(自​​定義列表框)... – 2012-02-22 16:47:40

回答

0

myYou不想添加ListBox。相反,使用TextBlock添加一個對象併爲此對象定義一個默認模板。

所以你定義的本地命名空間

xmlns:local="clr-namespace:MyNameSpace" 
在ressources

定義您的DataTemplate

<DataTemplate DataType="{x:type local:MyObjectType}"> 
     <TextBlock Text="{Binding ThePropertyIWant}" /> 
</DataTemplate> 

並在代碼中你只需要添加類型MyObjectType的對象添加到您的ListBox。

但更好的是,您將對象添加到ObservableCollection(Of MyObjectTYpe),並將該集合綁定到您的列表框。因此,當您將對象添加到集合中時,顯示會自動更新。