I have 2 classes:
class MultiSwitch : ListBox
{
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MultiSwitchItem;
}
protected override DependencyObject GetContainerForItemOverride( )
{
return new MultiSwitchItem( );
}
}
class MultiSwitchItem : ListBoxItem {...}
Basically, I want to place items depending on items count. So I subscribed to collection changed event and i'm trying to recieve MultiSwitchItem from Items collection. I found out that Items collection doesn't contain MultiSwitchItems. Then I found solution that looks like that:
private void RearrangeItems(
object sender,
NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
foreach (var item in Items)
{
MultiSwitchItem item2 = ItemContainerGenerator.ContainerFromItem(item) as MultiSwitchItem;
...
}
}
But item2 is always null. What am I doing wrong?
EDIT
Ok, I have a little progress with that. This issue appears only when control is initialized (so I think, wrappers for items are created not instantly). So the question now: how to force creating of MultiSwitchItem
s, or how to iterate over items AFTER wrappers was created?