Search code examples
c#wpfwindow

How do I open window inside another window after


I have a mainwindown and I want to open a window inside that stays on top of it. But it seems like the window I want inside is opened before the mainwindow opens. To solve this I need to open the window after initializecomponent of the mainwindow?

public partial class MainWindow : Window
{      
    public MainWindow ()
    {
        InitializeComponent();

        OpenProjectsView();
    }

    private void OpenProjectsView()
    {
        ProjectsView projectWindow= new ProjectsView();
        projectWindow.Owner = this;
        projectWindow.ShowDialog();
    }
}

Solution

  • Try this. It will launch your window after your main window is rendered.

    public MainWindow()
    {
        InitializeComponent();
        this.ContentRendered+= Window_ContentRendered;
    }
    
    private void Window_ContentRendered(object sender, RoutedEventArgs e)
    {
        OpenProjectsView();
    }