我是新來的WPF。我有一個List<string>
作爲我的ListBox's ItemsSource
的來源。最初,ListBox
顯示我的List<string>
確定中的所有Items
。但是,在嘗試向我的List<string>
添加一些字符串後,ListBox
不會更新這些更改。我使用Binding
綁定與ListBox
(視圖)的數據(後面),這裏是我的代碼:列表<string>未更新回ListBox的ItemsSource?
//Code behind
public MainWindow: Window {
public MainWindow(){
InitializeComponent();
Items = new List<string>(){"1","2","3"};//after loaded, all these values are displayed OK in my ListBox.
DataContext = this;
//Try clicking on a button to add new value
button1.Click += (s,e) => {
Items.Add("4");//But my ListBox stays the same without any update/changes.
};
}
public List<string> Items {get;set;}
}
//XAML
<ListBox ItemsSource={Binding Items}/>
能否請您指出我在做什麼錯在這裏,給我一個解決方案嗎?非常感謝你提前。
更改您的列表''到'的ObservableCollection '。 –
@CédricBignon謝謝你,它現在有效。 – Hopeless