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

    <tfoot id='BBLGU'></tfoot>

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

      1. <legend id='BBLGU'><style id='BBLGU'><dir id='BBLGU'><q id='BBLGU'></q></dir></style></legend>
          <bdo id='BBLGU'></bdo><ul id='BBLGU'></ul>
      2. WPF 列表框动态填充 - 如何让它刷新?

        时间:2023-10-07
        <tfoot id='bzIrb'></tfoot>

              • <legend id='bzIrb'><style id='bzIrb'><dir id='bzIrb'><q id='bzIrb'></q></dir></style></legend>
                  <bdo id='bzIrb'></bdo><ul id='bzIrb'></ul>
                • <small id='bzIrb'></small><noframes id='bzIrb'>

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

                  本文介绍了WPF 列表框动态填充 - 如何让它刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 WPF 新手,所以我认为这很简单.我有一个带有列表框和按钮的表单.在按钮的单击处理程序中,我迭代地执行生成字符串的操作,我想在获取字符串时将其放入列表框中.列表框的 xaml 就像

                  I am new to WPF, so I thought this was simple. I have a form with a listbox and a button. In the click handler for the button I do something iteratively that generates strings, which I want to put in the listbox as I get them. The xaml for the list box is like

                      <ListBox Height="87" Margin="12,0,12,10" Name="lbxProgress" VerticalAlignment="Bottom">
                          <ListBox.BindingGroup>
                              <BindingGroup Name="{x:Null}" NotifyOnValidationError="False" />
                          </ListBox.BindingGroup>
                      </ListBox>
                  

                  点击处理程序就像

                  private void btn_Click(object sender, RoutedEventArgs e)  
                  {  
                    List<String> lstrFiles= new List<String>(System.IO.Directory.GetFiles   ("C:\temp", "*.tmp");  
                  
                    foreach(string strFile in lstrFiles)  
                      lbxProgress.Items.Add(strFile);  
                  }  
                  

                  非常简单.由于我的实际操作很长,我希望列表框在每次添加时都更新 - 如何让框在每次添加时动态更新?

                  Pretty straightforward. Since my real operation is lengthy, I want the listbox to update as I do each one - how do I get the box to dynamically update on each addition?

                  推荐答案

                  创建一个 ObservableCollection 并将您的 ListBox.ItemsSource 设置为该集合.因为集合是可观察的,所以 ListBox 将随着其内容的变化而更新.

                  Create an ObservableCollection<string> and set your ListBox.ItemsSource to that collection. Because the collection is observable, the ListBox will update as its contents change.

                  但是,如果您的实际操作阻塞了 UI 线程,这可能会阻止 WPF 在操作完成之前更新 UI(因为 WPF 数据绑定基础结构没有机会运行).因此,您可能需要在后台线程上运行冗长的操作.在这种情况下,由于 WPF 跨线程限制,您将无法从后台线程更新 ObservableCollection(您可以更新属性,但不能更新集合).要解决此问题,请使用 Dispatcher.BeginInvoke() 在 UI 线程上更新集合,同时在后台线程上继续操作.

                  However, if your real operation is blocking the UI thread, this may prevent WPF from updating the UI until the operation completes (because the WPF data binding infrastructure doesn't get a chance to run). So you may need to run your lengthy operation on a background thread. In this case, you will not be able to update the ObservableCollection from the background thread due to WPF cross-threading restrictions (you can update properties, but not collections). To get around this, use Dispatcher.BeginInvoke() to update the collection on the UI thread while continuing your operation on the background thread.

                  这篇关于WPF 列表框动态填充 - 如何让它刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何获取列表框选定项的值 下一篇:c#终结器抛出异常?

                  相关文章

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

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

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

                      <tfoot id='jqjym'></tfoot>