How can i load my data in backgroundworker when using a CollectionViewSource? it throws an error because my CollectionViewSource is in UI thread.
This is all of my code (My background worker set in Xaml):
public partial class DepartmentsLookup : Window
{
private BackgroundWorker backgroundWorker;
private ObservableCollection<Department> AllDepartmentsData;
private ListCollectionView AllDepartmentsView;
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
IEnumerable<Department> data = null;
using (ArchiveEntities db = new ArchiveEntities())
{
data = db.Departments;
this.AllDepartmentsData = new ObservableCollection<Department>(data);
}
CollectionViewSource departmentsSource = (CollectionViewSource)this.FindResource("AllDepartmentsDataSource");
departmentsSource.Source = this.AllDepartmentsData;
this.AllDepartmentsView = (ListCollectionView)departmentsSource.View;
}
public DepartmentsLookup()
{
InitializeComponent();
backgroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker"));
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
}
thanx.
You have to work with WPF's Dispatcher object (Dispatcher.Invoke) to execute code on UI elements (when done from withni the BackgroundWorker). Maybe this article can give you some more information.
Or working with the WPF Dispatcher.