I have a WPF application with the main Window class called MainWindow.
Since I have other classes that need to access the Dispatcher of the UI thread to update bounded lists, I found this solution:
I made a static class:
static class UI
{
static public MainWindow window;
}
And added the following line in the app constructor:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UI.window = this;
...
Now I can access the components of my GUI everywhere by using UI.window.Dispatcher.Invoke().
The question is – is this a good programming practice?
Is there a better method of doing so?
Thank you
Update:
I seem to get the exception thrown only when I update an ObservableCollection which is bound to a 3rd party control. I have another static OC bound to a listbox (to display updated messages) and I can update that one from other threads without using the dispatcher.
How come?
Is it because its a static OC or is it related to the control?
If its only about the dispatcher, you can do this