Home > Enterprise >  WPF update observableCollection in UI thread
WPF update observableCollection in UI thread

Time:01-28

I have WinForms with WPF UserControl.

public partial class DynamicMediator : Form
{
        public DynamicMediator()
        {
            InitializeComponent();
            lmDynamicMediator = new LMDynamicMediator.MediatorMainWindow();            
            this.elementHost1.Child = this.lmDynamicMediator;
        }
        public MainWindowViewModel GetEditFormViewModel()
        {
            return lmDynamicMediator.Resources["ViewModel"] as MainWindowViewModel;
        }
}

I start a new process in my ViewModel after that I need to update my observableCollection in ViewModel I use

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => HasListMessages.Add(item)));

but I have exception like this

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread

if I use code like this

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => HasListMessages.Add(item)));

I have exception like this

System.Windows.Application.Current.get returned null

What I do wrong?

How I can get System.Windows.Application.Current in my ViwModel

CodePudding user response:

Calling Dispatcher.CurrentDispatcher in a thread that is not yet associated with a Dispatcher will create a new Dispatcher, which is not the Dispatcher of the UI thread.

Use the Dispatcher of the thread in which the view model instance is created. Keep it in a field of the view model class

public class MainWindowViewModel
{
    private readonly Dispatcher dispatcher;

    public MainWindowViewModel()
    {
        dispatcher = Dispatcher.CurrentDispatcher;
    }

    ...
}

and later use that field:

dispatcher.BeginInvoke(() => HasListMessages.Add(item));
  • Related