26

I have a WPF window containing a ListBox control that is populated when a button click method is executed.

XAML:

<ListBox Name="ThirdPartyListBox" ItemsSource="{Binding}" Margin="0,70,0,0">                      
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="C:\Users\Test\Desktop\Project\ACME-WPF\ACME-WPF\window-new-3.ico" Margin="5" Width="50"/>
                                <Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
                                <Button Name="ThirdPartyPostoneButton" Content="Postpone" Click ="PostponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
                                <TextBlock FontWeight="Bold" Text="{Binding Item2.Name}" Margin="12,25,0,0"/>
                                <TextBlock FontWeight="Bold" Text="{Binding Item2.RequiredVersion}" Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item2.CustomUIMessage}" Margin="10,25,0,0" TextWrapping="Wrap" Foreground="Red"/>
                                <TextBlock Text="You have used " Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item3.UsedDeferrals}" Margin="3,25,0,0"/>
                                <TextBlock Text=" of " Margin="3,25,0,0"/>
                                <TextBlock Text="{Binding Item2.MaxDefferals}" Margin="3,25,0,0"/>
                                <TextBlock Text=" deferrals for this update." Margin="3,25,0,0"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

C#:

 private void CheckforThirdPartyUpdatesButton_Click(object sender, RoutedEventArgs e)
    {
        CheckforThirdPartyUpdatesButton.IsEnabled = false;

        worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            MainEntry.checkFor3PUpdates();
        };

        worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
        {

        };

        worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
        {

            ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;
            CheckforThirdPartyUpdatesButton.IsEnabled = true;
        };

        worker.RunWorkerAsync();
    }

Everything up to this point functions as expected and the listbox is populated with multiple rows of items depending on how many items are in list ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;. However, if I interact with the listbox items at all, an InvalidOperationException is thrown with inner exception "An ItemsControl is inconsistent with its items source."

Can someone help me understand what's happening?

user3342256
  • 1,238
  • 4
  • 21
  • 35

5 Answers5

32

Such exceptions are thrown when an item's source has changed from another thread and ListBox doesn't receive a notification (CollectionChanged event) about ItemsSource being changed; so when it starts to do some work (like updating layout) it will see that Items are not equal to ItemsSource and throws an exception.

Since .NET 4.5, WPF provides a way to enable synchronization with a collection that changes from different threads. Try to use the EnableCollectionSynchronization method on your ItemsSource. See an example usage in this answer to a similar question

John Cummings
  • 1,949
  • 3
  • 22
  • 38
neptunao
  • 597
  • 5
  • 12
  • Using EnableCollectionSynchronization definitely help, but it does not stop all the exceptions. In my case I use Parallel.ForEach, which still causes the problem, although not as often. – DJ van Wyk Oct 20 '15 at 07:26
  • 4
    Would be great if this also had an example – tamj0rd2 Aug 02 '18 at 13:39
  • 1
    Rather irritated by this, so basically ObservableCollection is really only MainThreadObservableCollection. – AriesConnolly May 17 '20 at 14:27
30

You can simple call Refresh() method on your control where binding resources were changed:

myListBox.Items.Refresh();
Dragon
  • 303
  • 3
  • 5
8

I had the same error. It was caused by editing objects in the List, used as ItemsSource.

My solution was to replace the List with an ObservableCollection. According to .NET documentation an ObservableCollection:

"Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed."

SErnst
  • 121
  • 1
  • 4
1

I ran into the very same error message because I was clearing and then updating a list with UI binding multiple times (loop) in a single method.

The solution was to create a temporary list and assign the new list just once.

Not directly your problem but same error so I thought I'll mention it here...

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
-1

If your collection implements an INotifyCollectionChanged you can also sent a NotifyCollectionChangedAction.Reset message to basically say "I messed everything up, lets just start over."

John Melville
  • 3,613
  • 28
  • 30