I have a DataGrid like this:
class SearchFile
{
public string path { set; get; }
public int count { set; get; }
}
//...
files = new List<SearchFile>();
DataGridTextColumn col1 = new DataGridTextColumn();
DataGridTextColumn col2 = new DataGridTextColumn();
col1.Header = "File";
col2.Header = "count";
col1.Binding = new Binding("path");
col2.Binding = new Binding("count");
dataGrid1.Columns.Add(col1);
dataGrid1.Columns.Add(col2);
I do this to populate the items source:
foreach(var file in allFiles)
{
SearchFile sf = new SearchFile() { path=file, count=c };
files.Add(sf);
}
The above is a part of an event handler that will be launched multiple times. Where do I bind the files to the ItemsSource property so that it’s dynamically updated? I tried putting it after the foreach loop, but only the first run has any effect and the grid doesn’t change on subsequent runs (when allFiles is different).
Use an
ObservableCollection. It provides notifications when items get added, removed, or when the whole list is refreshed: