WPF speaker list dynamically populated - how to update it?

I am new to WPF, so I thought it was easy. I have a form with a list and a button. In the click handler for the button, I am doing iteratively something that generates the lines that I want to put into the list when I receive them. Xaml for the list looks like

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

and the click handler is similar to

 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 simple. Since my real operation is lengthy, I want the list to be updated, as I do each - how do I get a window for dynamic updating every time I add it?

+4
source share
3 answers

Create an ObservableCollection<string> and set your ListBox.ItemsSource to this list. Because the collection is observable, the ListBox will update as the content changes.

However, if your real operation blocks the user interface thread, this may prevent WPF from updating the user interface until the operation is completed (because the WPF data binding infrastructure is not able to run). Thus, you may need to perform a lengthy operation in the background thread. In this case, you cannot update the ObservableCollection from the background thread due to WPF streaming restrictions (you can update properties, but not collections). To get around this, use Dispatcher.BeginInvoke () to update the collection in the user interface thread while continuing to work in the background thread.

+5
source

do not use List <>, use ObservableCollection <> . Unlike a regular list, the Observable collection fires events whenever an item is added or removed, which means that any objects that are listening act accordingly - for example, updating your list to reflect new / deleted items.

If you need sorting, grouping, filtering, consider using CollectionView .

+4
source

To get the full answer, here is the code fragment minus some error handling code:

 namespace Whatever { public partial class MyWindow : Window { public delegate void CopyDelegate(); private string m_strSourceDir; // Source directory - set elsewhere. private List<string> m_lstrFiles; // To hold the find result. private string m_strTargetDir; // Destination directory - set elsewhere. private int m_iFileIndex; // To keep track of where we are in the list. private ObservableCollection<string> m_osstrProgress; // To attach to the listbox. private void CopyFiles() { if(m_iFileIndex == m_lstrFiles.Count) { System.Windows.MessageBox.Show("Copy Complete"); return; } string strSource= m_lstrFiles[m_iFileIndex]; // Full path. string strTarget= m_strTargetDir + strSource.Substring(strSource.LastIndexOf('\\')); string strProgress= "Copied \"" + strSource + "\" to \"" + strTarget + '\"'; try { System.IO.File.Copy(strFile, strTarget, true); } catch(System.Exception exSys) { strProgress = "Error copying \"" + strSource + "\" to \"" + strTarget + "\" - " + exSys.Message; } m_osstrProgress.Add(strProgress); ++m_iFileIndex; lbxProgress.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new CopyDelegate(CopyFiles)); } private void btnCopy_Click(object sender, RoutedEventArgs e) { m_lstrFiles= new List<String>(System.IO.Directory.GetFiles(m_strSourceDir, "*.exe")); if (0 == m_lstrFiles.Count) { System.Windows.MessageBox.Show("No .exe files found in " + m_strSourceDir); return; } if(!System.IO.Directory.Exists(m_strTargetDir)) { try { System.IO.Directory.CreateDirectory(m_strTargetDir); } catch(System.Exception exSys) { System.Windows.MessageBox.Show("Unable to create " + m_strTargetDir + ": " + exSys.Message); return; } } m_iFileIndex= 0; m_osstrProgress= new ObservableCollection<string>(); lbxProgress.ItemsSource= m_osstrProgress; lbxProgress.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new CopyDelegate(CopyFiles)); } } } 
+2
source

All Articles