ItemsControl与其项目源 - WPF Listbox不一致

use*_*256 17 c# wpf xaml listbox

我有一个WPF窗口,其中包含一个ListBox控件,该控件在执行按钮单击方法时填充.

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>
Run Code Online (Sandbox Code Playgroud)

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();
    }
Run Code Online (Sandbox Code Playgroud)

到目前为止的所有功能都按预期运行,并且列表框中填充了多行项目,具体取决于列表中的项目数量ThirdPartyListBox.DataContext = RegScan_ThirdParty.comparisonListWithState;.但是,如果我与列表框项进行交互,则抛出InvalidOperationException,内部异常"一个ItemsControl与其项目源不一致".

有人可以帮我理解发生了什么吗?

nep*_*nao 24

当项目的源已从另一个线程更改并且ListBox未收到CollectionChanged有关ItemsSource更改的通知(事件)时,将抛出此类异常; 所以当它开始做一些工作(比如更新布局)时,它会看到Items不等于ItemsSource并抛出异常.

从.NET 4.5开始,WPF提供了一种与从不同线程更改的集合启用同步的方法.尝试使用EnableCollectionSynchronization你的方法ItemsSource.

  • 如果这还有一个例子,那就太好了 (2认同)
  • 对此感到相当恼火,所以基本上 ObservableCollection 实际上只是 MainThreadObservableCollection。 (2认同)

小智 16

您可以在控件上简单地调用Refresh()方法,其中绑定资源已更改:

myListBox.Items.Refresh();
Run Code Online (Sandbox Code Playgroud)


小智 9

我有同样的错误。这是由编辑列表中用作 ItemsSource 的对象引起的。

我的解决方案是用 ObservableCollection 替换 List。根据 .NET 文档 ObservableCollection:

“表示一个动态数据集合,当添加、删除项目或刷新整个列表时提供通知。”