Search code examples
c#globalpartial-classes

How to make Window var global


I am using a piece of code that allows me to edit another window, e.g. a textbox located inside:

var MessageBox_Window = System.Windows.Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MessageBoxWindow) as MessageBoxWindow;

I need to be able to change the contents of this window constantly however I cannot put the var in partial class, and if I put it anywhere else it is not global. If I was to use:

ProjectName.MessageBoxWindow MessageBox_Window = System.Windows.Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MessageBoxWindow) as MessageBoxWindow;

in partial class, whenever I try to change the contents, I get a NullReferenceException.

Is there a way of making this code global so that it can be accessed from anywhere inside a window?


Solution

  • As Servy said, you shouldn't directly work with that window (and doing so from another Thread than the UI thread of that window is impossible).

    Consider having a class that contains all the parameters you want to share. Your MessageBoxWindow is responsible for drawing itself based on those parameters, while other windows are free to change them.