ItemsControl与它的项目源不一致-WPF列表框 [英] An ItemsControl is inconsistent with its items source - WPF Listbox
问题描述
我有一个WPF窗口,其中包含一个ListBox控件,该按钮在执行按钮click方法时填充.
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();
}
到目前为止,所有功能均按预期运行,并且列表框中填充了多行项目,具体取决于列表ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;
中的项目数.但是,如果我完全与列表框项目进行交互,则会抛出InvalidOperationException并带有内部异常"ItemsControl与它的项目源不一致".
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?
推荐答案
当某个项目的源已从另一个线程更改时,并且ListBox
没有收到有关ItemsSource
的通知(CollectionChanged
事件)时,将引发此类异常.被改变;因此,当它开始做一些工作(例如更新布局)时,会看到Items
不等于ItemsSource
并引发异常.
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.
从.NET 4.5开始,WPF提供了一种方法来启用与来自不同线程的集合的同步.尝试使用 EnableCollectionSynchronization
ItemsSource上的>方法.请参见此类似问题的答案
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
这篇关于ItemsControl与它的项目源不一致-WPF列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!