Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listBox.ItemContainerGenerator.ContainerFromItem() returns null to the NEW item added in the listbox

Tags:

wpf

This is my first post here so I hope you could help me with my problem regarding WPF.

I have a listbox that is bind with an ObservableCollection:

    public ObservableCollection<DeviceSetting> DeviceSettings
                {
                    get { return _deviceSettings; }
                    set { _deviceSettings = value; }
                }

   <ListBox ItemTemplate="{StaticResource IPItemTemplate}" Name="listBoxAddresses" SelectionMode="Extended" ItemsSource="{Binding Path=TestSetting.DeviceSettings, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
     ItemContainerStyle="{StaticResource ContainerStyle}" />

The situation here is, I would like to know if a new item has been added to the list so what I did was create a CollectionChanged event:

TestSetting.DeviceSettings.CollectionChanged += mListBox_CollectionChanged;  

private void mListBox_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
   if (e.Action == NotifyCollectionChangedAction.Add)
   {
       for (int i = 0; i < TestSetting.DeviceSettings.Count; i++){

       ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(listBoxAddresses.Items[i]));


        if (!TestSetting.DeviceSettings[i].IsNetwork && DeviceDiscovery.IsSelected)
                  myListBoxItem.IsEnabled = false;

        else if (TestSetting.DeviceSettings[i].IsNetwork && !DeviceDiscovery.IsSelected)
                  myListBoxItem.IsEnabled = false;
        else
                  myListBoxItem.IsEnabled = true;

     }
 }

But a problem occurs at this statement:

ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(listBoxAddresses.Items[i]));

Everytime I added a new item, the statement above always returns null so the new item that was added was not checked if would be enabled or not. Is there a way for this statement to return the correct ListBoxItem that I need?

like image 717
JŸR Avatar asked Nov 08 '11 05:11

JŸR


1 Answers

You're handling the underlying collections CollectionChanged event. Just because the collection was changed does not mean that the item has been rendered and the UIElement is ready.

Register for the ItemsGenerator.StatusChanged event which should guarantee that the UIElement is ready.

like image 194
Aaron McIver Avatar answered Sep 28 '22 17:09

Aaron McIver