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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With