Reputation: 601
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();
}
}
Upvotes: 0
Views: 5308
Reputation:
try this code
projectWindow.IsMdiContainer = True;
projectWindow.MdiParent = this;
projectWindow.Show();
Upvotes: 1
Reputation: 3290
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
OpenProjectsView();
}
private void OpenProjectsView()
{
ProjectsView projectWindow= new ProjectsView();
projectWindow.Owner = this;
projectWindow.TopMost = true;
projectWindow.Show();
}
}
This will make the second window sit on top of the first, but because you create and show it during your parent window constructor, it will always appear (get instantiated) before your parent window.
This method allows that to happen, whilst you can still interact with both windows (Show()
rather than ShowDialog()
).
As others have mentioned, you can instantiate your window in an event, rather than the constructor, but that depends on exactly what you need ... your question is a little ambiguous! :O)
Upvotes: 0
Reputation: 3284
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();
}
Upvotes: 0
Reputation: 1718
Call OpenProjectsView( ) in Form.Activated event.
private void MainWindow_Activated(object sender, System.EventArgs e)
{
OpenProjectsView();
}
Upvotes: 1