2009-07-30 68 views
0

嗨,我有与ItemContainerGenerator问题在Silverlight 3Silverlight项目容器生成器状态?

在我的派生自定义列表框我加入这个处理程序:

ItemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged); 

每次项目更改我得到一个回电话。迄今为止很好。

现在在ItemContainerGenerator_ItemsChanged我想做的事情取决于这样的动作不同的事情:

  switch (e.Action) 
     { 
      case NotifyCollectionChangedAction.Add: 
       Debug.WriteLine("items added"); 
       break; 
      case NotifyCollectionChangedAction.Remove: 
       Debug.WriteLine("items removed"); 
       break; 
      case NotifyCollectionChangedAction.Replace: 
       Debug.WriteLine("items removed"); 
       break; 
      case NotifyCollectionChangedAction.Reset: 
       Debug.WriteLine("items reset, itemscount:" + this.Items.Count); 

       CalcMaxColumnWidths(); 

       break; 
      default: 
       break; 
     } 

一切工作正常,直到如今。在CalcMaxColumnWidths()我打电话时动作复位我这样做:

foreach (ListBoxItem item in ItemsControlExtensions.GetContainers(this)) 
     { //some code here } 

这里的问题:

我试图与GetContainers获得()方法返回null容器,即使Items.Count是正确的值(60)。

在WPF中有一个ItemContainerGenerator.Status,所以我们可以等到它被设置为完成然后迭代容器。

我该如何在Silverlight中解决这个问题? (没有任何计时器!)

感谢您的帮助!

回答

0

我没有在ListBox本身做所有的魔术,而是创建了一个自定义Panel并将其用作ItemPanel。 在面板的测量和排列周期中,我现在计算列宽。在这些循环中,所有的容器都已经创建好了。问题解决了。

但它仍然认为ItemContainerGenerator中缺少状态。