Note that there are some explanatory texts on larger screens.

plurals
  1. POlistBox.ItemContainerGenerator.ContainerFromItem() returns null to the NEW item added in the listbox
    primarykey
    data
    text
    <p>This is my first post here so I hope you could help me with my problem regarding WPF.</p> <p>I have a listbox that is bind with an ObservableCollection:</p> <pre><code> public ObservableCollection&lt;DeviceSetting&gt; DeviceSettings { get { return _deviceSettings; } set { _deviceSettings = value; } } &lt;ListBox ItemTemplate="{StaticResource IPItemTemplate}" Name="listBoxAddresses" SelectionMode="Extended" ItemsSource="{Binding Path=TestSetting.DeviceSettings, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" ItemContainerStyle="{StaticResource ContainerStyle}" /&gt; </code></pre> <p>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:</p> <pre><code>TestSetting.DeviceSettings.CollectionChanged += mListBox_CollectionChanged; private void mListBox_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { for (int i = 0; i &lt; TestSetting.DeviceSettings.Count; i++){ ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(listBoxAddresses.Items[i])); if (!TestSetting.DeviceSettings[i].IsNetwork &amp;&amp; DeviceDiscovery.IsSelected) myListBoxItem.IsEnabled = false; else if (TestSetting.DeviceSettings[i].IsNetwork &amp;&amp; !DeviceDiscovery.IsSelected) myListBoxItem.IsEnabled = false; else myListBoxItem.IsEnabled = true; } } </code></pre> <p>But a problem occurs at this statement:</p> <pre><code>ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(listBoxAddresses.Items[i])); </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload