I am trying to access a List on a view model from a background worker, but am getting errors because I am going cross thread…
This is the problem method on the viewmodel: (I am getting the exception the first line in the function (SMMainWindow window …))
public static MainWindowViewModel GetMainWindowViewModel() {
SMMainWindow window = (SMMainWindow)System.Windows.Application.Current.MainWindow;
if (window != null) {
return (MainWindowViewModel)window.DataContext;
}
return null;
}
Any ideas? Sample code would be appreciated
It helps tremendously if you setup your ViewModel with an instance of your Window’s Dispatcher. If you have this, then you can just use Dispatcher.Invoke to fetch or set items within the ViewModel.