我試圖將項目動態添加到組合框。添加組合框項目中的異常
但它拋出了「使用ItemsSource時操作無效,使用ItemsControl.ItemsSource訪問和修改元素」。
namespace Trainning
{
public partial class ComboBox : Window
{
int intex_count;
public ComboBox()
{
this.InitializeComponent();
add_items();
}
public List<object> add_items()
{
List<object> items = new List<object>();
items.Add("chandru");
items.Add(83);
com_add_remove.ItemsSource = items;
com_add_remove.SelectedIndex = 0;
return items;
}
private void btn_add_Click(object sender, RoutedEventArgs e)
{
com_add_remove.Items.Add(txt_item.Text);
intex_count = com_add_remove.Items.Count;
com_add_remove.SelectedIndex = intex_count - 1;
}
你不能把源和變化同時如果你想做和做源代碼的變化,並且簡單地將源代碼更新爲像observablecollection一樣。 – JSJ