Xexolas
Xexolas

Reputation: 239

Switching between two windows in WPF

Suppose i have two different windows in WPF application Window1 and Window2.

Based on some actions from Window1, Window2 pop up for a while, and also based on some actions in Window1, Window2 should close and view returns to Window1 with it's state before Window2 appears.

With it's state i mean with all the content in all the controls that exists in Window1.

To achieve the switching i used

ShowDialog();

which i was looking exactly because i need Window1 to freeze while Window2 is on.

Right now my problem i can't return to Window1 with it's content.

MainWindow (Window1).xaml

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="193,130,0,0" Click="Button_Click"/>
</Grid></Window>

Button_Click

private void Button_Click(object sender, RoutedEventArgs e)
{
    Window1 window1 = new Window1();
    window1.ShowDialog();

    //Actions

    window1.Close();
}

and the Window1 xaml is a normal one i didn't change anything except that i made the

WindowStyle=None

so i can't exit it with the right top exit button

Upvotes: 4

Views: 13529

Answers (4)

Nam.tsh
Nam.tsh

Reputation: 31

This topic is too old but perhaps this is what you were talking about?

// Currently in Window1, want to open window2 and make window1 not visible
private void Change_Click(object sender, RoutedEventArgs e)
        {
            Window2 mainWindow = new Window2();
            Visibility = Visibility.Hidden;
            Window2.Show();
        }

Upvotes: 3

user3614355
user3614355

Reputation:

Here's what commonly worked for me:

Dim TheAboutWindow As New winAbout
TheAboutWindow.Owner = Me
TheAboutWindow.ShowDialog()
TheAboutWindow = Nothing

Sometimes I use My.Windows.winName when I don't want to load stuff over and over again, but you need to cancel the close and hide the window somehow.

But there is one thing that is great about My.Windows.winName, you don't need to remember the variable name and it's usually accessible for any open window. (Not sure how that works when two instances are open though.)

That means you can do My.Windows.winMain.Title = "OMG Jawj. Did you see that?"

My.Windows.winHost.Owner = Me
My.Windows.winHost.ShowDialog()

You can/should omit the owner line if you want to perform minimizing features from the child and show the child again on max/normal.

Upvotes: 1

Alexander Bell
Alexander Bell

Reputation: 7918

Your question and business logic is a bit unclear, so the answer is based on a qualified guess: in order to open the second WPF Window (window2) in a Dialog Mode, use the following generic code:

Window2 _window2 = new Window2()
{
    Title = "Modal Dialog Window",
    Topmost = true,
    ResizeMode = ResizeMode.NoResize,
    ShowInTaskbar = false,
    Owner = this
};
_window2.ShowDialog();

In order to close that second window2 , use either the standard "Close" Button in the Upper-Right Control Box, or add the Button to that window2 and put this.Close() statement into that Button Click event.

Edited per your additional comments: If you show the second window as a Modal Dialog then you are blocking the Owner Window (like popping the MessageBox, for example). Please clarify your business logic because it's very unclear why you need to open the second window and then close it from the main one. What kind of "Actions" you put between Opening and Closing that second window in the Button Click event of main window? You may consider opening a second window in normal mode, then you can close it from main Window using for example, some Button control placed on the first window. It seems you have to refine the business logic.

Hope this may help.

Upvotes: 2

Сергей
Сергей

Reputation: 156

If you are using MVVM you can use this:

public static class DialogCloser
{
    public static readonly DependencyProperty DialogResultProperty =
        DependencyProperty.RegisterAttached(
            "DialogResult",
            typeof(bool?),
            typeof(DialogCloser),
            new PropertyMetadata(DialogResultChanged));

    private static void DialogResultChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var window = d as Window;
        if (window != null)
            window.DialogResult = e.NewValue as bool?;
    }
    public static void SetDialogResult(Window target, bool? value)
    {
        target.SetValue(DialogResultProperty, value);
    }
}

And in XAML:

 <Window x:Class="Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/
    xmlns:xc="clr-namespace:Window2"
    xc:DialogCloser.DialogResult="{Binding DialogResult}"

Then in Model you can use:

 public bool? DialogResult
    {
        get;
        set;
    }

Upvotes: 2

Related Questions