Viewed   8.2k times

How one can show dialog window (e.g. login / options etc.) before the main window?

Here is what I tried (it apparently has once worked, but not anymore):

XAML:

<Application ...
    Startup="Application_Startup">

Application:

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Window1 myMainWindow = new Window1();
        DialogWindow myDialogWindow = new DialogWindow();
        myDialogWindow.ShowDialog();
    }
}

Outcome: myDialogWindow is shown first. When it is closed, the Window1 is shown as expected. But as I close Window1 the application does not close at all.

 Answers

3

Here's the full solution that worked for me:

In App.xaml, I remove the StartupUri stuff, and add a Startup handler:

<Application x:Class="MyNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="ApplicationStart">
</Application>

In App.xaml.cs, I define the handler as follows:

public partial class App
{
    private void ApplicationStart(object sender, StartupEventArgs e)
    {
        //Disable shutdown when the dialog closes
        Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

        var dialog = new DialogWindow();

        if (dialog.ShowDialog() == true)
        {
            var mainWindow = new MainWindow(dialog.Data);
            //Re-enable normal shutdown mode.
            Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
            Current.MainWindow = mainWindow;
            mainWindow.Show();
        }
        else
        {
            MessageBox.Show("Unable to load data.", "Error", MessageBoxButton.OK);
            Current.Shutdown(-1);
        }
    }
}
Sunday, September 4, 2022
 
5

Did you try showing your window using the ShowDialog method?

Don't forget to set the Owner property on the dialog window to the main window. This will avoid weird behavior when Alt+Tabbing, etc.

Tuesday, October 18, 2022
 
1

pass a parameter to the loginwindow of type MainWindow. That allows the Login window to have a reference to the MainWindow:

this.Hide();
Login li = new Login(this);
li.Show();

And the login window:

private MainWindow m_parent;
public Login(MainWindow parent){
    m_parent = parent;
}

//Login Succesfull function

private void Succes(){
    m_parent.Show();
}
Monday, October 3, 2022
 
5

You should set Application.ShutdownMode to OnExplicitShutdown (msdn).

Example:

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml"
    ShutdownMode="OnExplicitShutdown"
    >
</Application>
Sunday, December 25, 2022
3

To set a Window's Default button

Set your default button's IsDefault property to true.

Note that you can also set a Window's Cancel button by setting a button's IsCancel property to true.


To set the Selected (focused) button in a Window

If you want to select a particular button then use the Focus method like this:

yourButton.Focus();

You might do this when a Window loads (in the Window_Loaded event).

To select a particular button when your Window opens make sure its IsTabStop property is set to true and ensure its TabIndex property is lower than any other control on the Window.

Friday, December 9, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :