2012-05-24 47 views
0

我想通過下面的代碼得到一個列表框項目。基本上我想要做的是創建一個tempdatelist,然後將Listbox的itemsource設置爲tempdatelist。Windows Phone 7 Listbox.ItemContainerGenerator.ContainerFromIndex(i)返回null?

if (App.Saveholidayplan[App.selectedlistindex].travel.Count > 0)  
    foreach (var dictobj in App.Saveholidayplan[App.selectedlistindex].travel[0].DummyRepository)     
     tempdatelist.Add(dictobj.Key); 


lst_mainlist.ItemsSource = tempdatelist; 

ListBoxItem item = this.lst_mainlist.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem; 

//* item is alway null, that is the problem 
if(item != null) 

但在上面的代碼項目retuns空。

當我在網上查詢一些建議致電ItemContainerGenerator.StatusChanged事件。

但我無法在WP7中找到此事件? 在WP7中是否存在StatusChanged事件,如果不存在替代方案?

+0

顯示更多的代碼的youre,所以我們可以看到你已經包括 –

+0

編輯我的問題與代碼上下文中的片段的背景下 –

+1

一旦你得到它,你會用'item'做什麼?我不確定你爲什麼在這裏搞亂了ItemContainerGenerator。 – Robaticus

回答

1

我也有這個問題。解決的辦法是等待UI通過使用調度,這樣渲染:

this.Dispatcher.BeginInvoke(() => 
{ 
    ListBoxItem item = this.lst_mainlist.ItemContainerGenerator.ContainerFromIndex(i); 
    //... 
});