views:

402

answers:

1

Hey guys, I'm having problems with the ItemContainerGenerator in Silverlight 3.

In my derived Custom ListBox I'm adding this handler:

ItemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);

Every time the Items change I'm getting a call back. Fine so far.

Now in the ItemContainerGenerator_ItemsChanged I want to do different things depending on the action like this:

            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;
        }

Everything works fine until now. In the CalcMaxColumnWidths() i call when the action is reset I do this:

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

Here's the problem:

The Containers I'm trying to get with the GetContainers() method returns null, even though the Items.Count is the correct value (60).

In WPF there was a ItemContainerGenerator.Status, so we could wait until that was set to Done and then iterate over the containers.

How can i solve this in Silverlight? (without any Timers!)

Thanks for your help!

A: 

Instead of doing all the magic in the ListBox itself, I ended up creating a custom Panel and using it as the ItemPanel. In the Panel's Measure and Arrange cycles I calculate the Column Widths now. In these cycles all the containers are already created. Problem solved.

But it still sucks that the Status is missing from the ItemContainerGenerator.

ppx