Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF best practice to get current MainWindow instance?

Tags:

wpf

instance

I got a warning that this may be a subjective question and might be closed, but I'm going to ask anyway.

I'm basically trying to access a button on my MainWindow in a WPF application from a UserControl that gets loaded up from within the MainWindow.

I'm currently accessing it like this from the UserControl's code behind:

((MainWindow)Application.Current.MainWindow).btnNext

But it does look messy, and from what I've read is not considered a best practice. Anyone able to provide an answer that constitutes a best practice for Accessing controls / properties from the current instance of a MainWindow - or any other active windows / views for that matter?

like image 219
ganjeii Avatar asked Dec 26 '16 15:12

ganjeii


People also ask

How do I change the startup window in WPF?

If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.

How can I access a control in WPF from another class or window?

If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean. Save this answer.

How do you call a window in WPF?

Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK. Step 2: Create a button using the following code or drag and drop a button from the ToolBox of Visual Studio 2013.


1 Answers

You can get a reference to the parent window of the UserControl using the Window.GetWindow method. Call this once the UserControl has been loaded:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        this.Loaded += (s, e) =>
        {
            MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
            if (parentWindow != null)
            {
                //...
            }
        };
    }
}

You could also access all open windows using the Application.Current.Windows property:

MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

Which one to use depends on your requirements. If you want a reference to the application's main window for some reason, you could stick with your current approach. If you want a reference to the parent window of the UserControl, using the Window.GetWindow method would be better.

The best practice is generally to use the MVVM design pattern and bind UI controls to source properties of a view model that may be shared by several views. But that's another story. You could refer to the following link for more information about the MVVM pattern: https://msdn.microsoft.com/en-us/library/hh848246.aspx

like image 104
mm8 Avatar answered Oct 30 '22 21:10

mm8