How to associate a collection with a ListView in WPF

I have a program that looks for a directory for files matching certain criteria. This search process takes a long time , so I have to call it asynchronously. When the search algorithm finds a file, it raises an event. My MainWindow instance listens for this event and needs to update the GUI. How to link these "added" files with ListView ? I decided that I could use an instance of ObservableCollection<FileInfo> , but I could not figure out how to link it.

I removed all unnecessary controls and code. Here are two matching files.

MainWindow.xaml:

 <Window x:Class="Example.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CR Search" Height="395" Width="525"> <Grid> <ListView x:Name="Results"> <ListView.View> <GridView> <GridViewColumn Header="Filename"/> <GridViewColumn Header="Directory"/> </GridView> </ListView.View> </ListView> </Grid> </Window> 

MainWindow.xaml.cs:

 using System.IO; using System.Threading.Tasks; public partial class MainWindow { private SearchLogic _backgroundSearch; private async void Search(object sender, RoutedEventArgs e) { // TODO: clear Results _backgroundSearch = new SearchLogic("", new DirectoryInfo("C:\")); _backgroundSearch.FileAdded += FileAdded; await Task.Run(new Action(_backgroundSearch.Search)); } private void FileAdded(object sender, FileAddedEventArgs eventArgs) { // TODO: add eventArgs.File to Results // eventArgs.File is an instance of FileInfo } } 
+6
source share
1 answer

Here is a simple example

Your xaml

 <Window x:Class="WpfApplication10.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="525" Height="350" Loaded="Window_Loaded"> <Grid> <ListBox ItemsSource="{Binding FileNames}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <Label>Name</Label> <TextBlock Text="{Binding Name}"/> <Label>Modified</Label> <TextBlock Text="{Binding LastModified}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> 

Your code for

 public partial class MainWindow : Window { public class FileInfo { public string Name { get; set; } public DateTime LastModified { get; set; } public FileInfo(string name) { Name = name; LastModified = DateTime.Now; } } ObservableCollection<FileInfo> mFileNames = new ObservableCollection<FileInfo>(); public ObservableCollection<FileInfo> FileNames { get { return mFileNames; } } public MainWindow() { DataContext = this; InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { ThreadPool.QueueUserWorkItem((x) => { while (true) { Dispatcher.BeginInvoke((Action)(() => { mFileNames.Add(new FileInfo("X")); })); Thread.Sleep(500); } }); } } 

If you run this problem, you will notice that the list is updated every half second by a new item. It’s basically important to note that the ObservableCollection can only be updated from the user interface thread, so if you are reorganizing the above code, you need to somehow use the manager of the current user interface thread to update it.

+12
source

All Articles