Windows Phone 7 Listbox.ItemContainerGenerator.ContainerFromIndex(i)返回null?

时间:2012-05-24 11:40:09

标签: c# silverlight windows-phone-7.1

我想通过下面的代码获取一个Listbox项目。基本上我要做的是创建一个tempdatelist,然后将Listbox的itemsource设置为tempdatelist。

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)

但是在上面的代码项中返回null。

当我在线查看时,有人建议拨打ItemContainerGenerator.StatusChanged活动。

但是我无法在WP7中找到这个事件? WP7中是否有StatusChanged事件,如果没有,可以选择什么?

1 个答案:

答案 0 :(得分:1)

我也有这个问题。解决方案是使用调度程序等待UI呈现,如下所示:

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