Tuesday, June 14, 2011

WPF - TreeView list update

For updating a tree view (after removing elements) the tree view did not change - all elements still appeared on it.

Solution:

Use an ObservableCollection for the tree view items:

ObservableCollection fileList = new ObservableCollection();


to add:

fileList.Add(file);
list.ItemsSource = fileList;


and remove items like this:

ObservableCollection auxList = new ObservableCollection();

foreach (string item in fileList)
{
if (!TheListview.SelectedItems.Contains(item))
{
auxList.Add(item);
}
}
fileList = auxList;
TheListview.ItemsSource = fileList;


I've tried items refresh, setting the collection to invalid but none worked.

No comments: