<i id='eLcCr'><tr id='eLcCr'><dt id='eLcCr'><q id='eLcCr'><span id='eLcCr'><b id='eLcCr'><form id='eLcCr'><ins id='eLcCr'></ins><ul id='eLcCr'></ul><sub id='eLcCr'></sub></form><legend id='eLcCr'></legend><bdo id='eLcCr'><pre id='eLcCr'><center id='eLcCr'></center></pre></bdo></b><th id='eLcCr'></th></span></q></dt></tr></i><div id='eLcCr'><tfoot id='eLcCr'></tfoot><dl id='eLcCr'><fieldset id='eLcCr'></fieldset></dl></div>
  • <small id='eLcCr'></small><noframes id='eLcCr'>

  • <tfoot id='eLcCr'></tfoot>

    <legend id='eLcCr'><style id='eLcCr'><dir id='eLcCr'><q id='eLcCr'></q></dir></style></legend>

      <bdo id='eLcCr'></bdo><ul id='eLcCr'></ul>
      1. ItemsControl 与其项目源不一致 - WPF Listbox

        An ItemsControl is inconsistent with its items source - WPF Listbox(ItemsControl 与其项目源不一致 - WPF Listbox)
      2. <legend id='ZBSlO'><style id='ZBSlO'><dir id='ZBSlO'><q id='ZBSlO'></q></dir></style></legend>
        • <i id='ZBSlO'><tr id='ZBSlO'><dt id='ZBSlO'><q id='ZBSlO'><span id='ZBSlO'><b id='ZBSlO'><form id='ZBSlO'><ins id='ZBSlO'></ins><ul id='ZBSlO'></ul><sub id='ZBSlO'></sub></form><legend id='ZBSlO'></legend><bdo id='ZBSlO'><pre id='ZBSlO'><center id='ZBSlO'></center></pre></bdo></b><th id='ZBSlO'></th></span></q></dt></tr></i><div id='ZBSlO'><tfoot id='ZBSlO'></tfoot><dl id='ZBSlO'><fieldset id='ZBSlO'></fieldset></dl></div>
          <tfoot id='ZBSlO'></tfoot>

                <bdo id='ZBSlO'></bdo><ul id='ZBSlO'></ul>

                <small id='ZBSlO'></small><noframes id='ZBSlO'>

                  <tbody id='ZBSlO'></tbody>

                  本文介绍了ItemsControl 与其项目源不一致 - WPF Listbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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:UsersTestDesktopProjectACME-WPFACME-WPFwindow-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?

                  解决方案

                  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

                  这篇关于ItemsControl 与其项目源不一致 - WPF Listbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                    <bdo id='h34oQ'></bdo><ul id='h34oQ'></ul>
                          <tbody id='h34oQ'></tbody>

                        1. <i id='h34oQ'><tr id='h34oQ'><dt id='h34oQ'><q id='h34oQ'><span id='h34oQ'><b id='h34oQ'><form id='h34oQ'><ins id='h34oQ'></ins><ul id='h34oQ'></ul><sub id='h34oQ'></sub></form><legend id='h34oQ'></legend><bdo id='h34oQ'><pre id='h34oQ'><center id='h34oQ'></center></pre></bdo></b><th id='h34oQ'></th></span></q></dt></tr></i><div id='h34oQ'><tfoot id='h34oQ'></tfoot><dl id='h34oQ'><fieldset id='h34oQ'></fieldset></dl></div>
                          <legend id='h34oQ'><style id='h34oQ'><dir id='h34oQ'><q id='h34oQ'></q></dir></style></legend>

                          <small id='h34oQ'></small><noframes id='h34oQ'>

                          <tfoot id='h34oQ'></tfoot>