积极答复者
如何把一個 data entity 轉換成ObservableCollection

问题
-
請問各位先進,比較以下兩段簡單的程序片段
下列的做法是OK的,DataGrid1 裏有資料出現,
LoadOperation<Product> loadOP = DS.Load(DS.GetProductQuery());
dataGrid1.ItemsSource = loadOP.Entities;可是以下的做法卻不行, dataGrid1 裏沒有資料 ?? 為什麼 ObservableCollection<Product> 裏沒有資料;
LoadOperation<Product> loadOP = DS.Load(DS.GetProductQuery());
ObservableCollection<Product> _products = new ObservableCollection<Product>(loadOP.Entities);dataGrid1.ItemsSource = _products;
上述的問題,我想不出來,故求教各位先進.
- 已编辑 joseph9406 2012年12月2日 9:27
答案
-
你好,
》》 LoadOperation<Product> loadOP = DS.Load(DS.GetProductQuery());
這裏的LoadOperation代表一個異步操作,後面的dataGrid1.ItemsSource獲取的此異步操作的結果
》》loadOP.Entities 裏是有資料的
loadOP.Entities裏的資料是通過LoadOperation這個異步操作后加載進來的,你可以看一下以下的截圖, loadOP.Entites是空的
Mark Yu - MSFT
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 iwpfModerator 2012年12月7日 9:20
- 已标记为答案 joseph9406 2012年12月7日 9:45
全部回复
-
你好 joseph9406,
LoadOperation 是異步的,dataGrid1裏面是不會有資料的。
Mark Yu - MSFT
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
你好,
》》 LoadOperation<Product> loadOP = DS.Load(DS.GetProductQuery());
這裏的LoadOperation代表一個異步操作,後面的dataGrid1.ItemsSource獲取的此異步操作的結果
》》loadOP.Entities 裏是有資料的
loadOP.Entities裏的資料是通過LoadOperation這個異步操作后加載進來的,你可以看一下以下的截圖, loadOP.Entites是空的
Mark Yu - MSFT
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 iwpfModerator 2012年12月7日 9:20
- 已标记为答案 joseph9406 2012年12月7日 9:45
-
另外再作一點補充,你可以添加以下代碼測試一下:
LoadOperation<Users> loadOp = this.ds2.Load(this.ds2.GetUsersQuery()); dataGrid1.ItemsSource = loadOp.Entities; List<Users> temp = loadOp.Entities.ToList<Users>(); foreach(Users s in temp) { //MessageBox.Show(s.ToString()); MessageBox.Show(s.AuthType.ToString()+s.EntityActions.ToString()+s.EntityConflict.ToString()+s.EntityState.ToString()); }
在這些代碼上設置斷點,你可以發現當你設置了dataGrid1.ItemsSource = loadOp.Entities,程序執行這一步以及後面的foreach完畢后,程序才會執行到Entity里的set。
Mark Yu - MSFT
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.