2009-06-11 18 views
0

我通過DataGrid.ItemSource屬性將IEnumerable集合傳遞給WPF DataGrid。但是當我試圖更改代碼中的集合項時,它不會更新DataGrid。 爲什麼?WPF DataGrid.ItemSource

回答

3

您需要綁定到實現INotifyCollectionChanged接口的類型,以便它提供數據綁定可用於監視何時添加或刪除項目的事件。這在WPF的最佳類型的ObservableCollection <>,它有一個構造函數會接受你的IEnumerable:

ObservableCollection<string> collection = new ObservableCollection<string>(iEnumerableobject); 
dataGrid.ItemSource = collection; 
collection.Add("Wibble"); 

將正常更新。


從您的意見到另一個答案,它看起來像你需要從UI線程內調用添加調用。不知道更詳細的代碼,我不知道爲什麼你需要做到這一點,但讓我們假設你是從一個服務在後臺獲取數據:

private ObservableCollection<string> collection; 

public void SetupBindings() 
{ 
    collection = new ObservableCollection<string>(iEnumerableobject); 
    dataGrid.ItemSource = collection; 
    //off the top of my head, so I may have this line wrong 
    ThreadPool.Queue(new ThreadWorkerItem(GetDataFromService)); 
} 

public void GetDataFromService(object o) 
{ 
    string newValue = _service.GetData(); 

    //if you try a call add here you will throw an exception 
    //because you are not in the same thread that created the control 
    //collection.Add(newValue); 

    //instead you need to invoke on the Ui dispatcher 
    if(Dispather.CurrentDispatcher.Thread != Thread.CurrentThread) 
    { 
     Dispatcher.CurrentDispatcher.Invoke(() => AddValue(newValue)); 
    } 
} 

public void AddValue(string value) 
{ 
    //because this method was called through the dispatcher we can now add the item 
    collection.Add(value); 
} 

正如我說的,我不有一個IDE可以交付,所以這可能不會編譯,但會指向正確的方向。

根據你在後臺做什麼確切的任務,可能有更好的方法來做到這一點。我上面的示例使用backgroundworker將更容易實現,因此您可能也想閱讀該示例。

+0

+1爲男人,而不僅僅是在答案中使用「Wibble」。我說得好! 。 。 。是的,今天下午我感覺有點奇怪。 。 。 – 2009-06-11 16:08:44

+0

我更改爲ObservableCollection,但它仍然沒有區別。這是顯示正確的數據,當我第一次啓動一個程序,但從未更新網格後,然後......我做錯了什麼 – Agzam 2009-06-11 16:10:21

1

您需要改爲使用ObservableCollection。 (或使自己的類包裝的收集和實現INotifyPropertyChanged接口)

0

您也可以使用實現INotifyCollectionChanged界面的集合,如果你不能使用的ObservableCollection出於某種原因...