WPF Window (or do I mean View) Navigation (or do I mean switching, opening, closing…) part 2 #wpf

Following on from my post WPF Window (or do I mean View) Navigation (or do I mean switching, opening, closing…) I thought I would quickly cover a slight variation on the open and close view I was doing. Essentially you might want to merely temporarily show a different view or quickly switch between views without recreating it.

To do this I updated my button and added a new button like so:

<Button Content="Switch" HorizontalAlignment="Left" Margin="0,0,0,160" VerticalAlignment="Bottom" Width="75" Command="{Binding Switch}"/>
        <Button Content="Switch Hide" HorizontalAlignment="Left" Margin="0,0,0,190.04" VerticalAlignment="Bottom" Width="75" Command="{Binding SwitchHide}"/>

The old button is now called Switch and the new button SwitchHide. I bound SwitchHide button to the SwitchHide command which is defined like so:

SwitchHide = new RelayCommand(() => { ((App)App.Current).SwitchHideWindow(); });
public RelayCommand SwitchHide { get; set; }

I then wrote the code for SwitchHideWindow in App.xaml.cs and updated the code for SwitchWindow like so:

        internal void SwitchWindow()
        {
            // If we have extra windows the close any hidden ones
            if (Windows.Count > 1)
            {
                foreach (Window w in Windows)
                {
                    if (!w.IsVisible)
                    {
                        w.Close();
                        w.DataContext = null; // Ensure w no longer has a binding to the ViewModel, so we can reuse it at our leisure
                    }
                }
            }

            Window w0 = Windows[0];
            Window w1;

            if (w0 is MainWindow)
            {
                w1 = new AnotherWindow();
            }
            else
            {
                w1 = new MainWindow();
            }

            w1.Show();
            w0.Close();
            w0.DataContext = null; // Ensure w0 no longer has a binding to the ViewModel, so we can reuse it at our leisure
        }

        internal void SwitchHideWindow()
        {
            Window w0 = Windows[0];
            Window w1;

            // Ensure we have two windows
            if (Windows.Count < 2)
            {
                if (w0 is MainWindow)
                {
                    w1 = new AnotherWindow();
                }
                else
                {
                    w1 = new MainWindow();
                }
            }
            else
            {
                w1 = Windows[1];
            }

            if (w1.IsVisible)
            {
                w0.Show();
                w1.Hide();
            }
            else
            {
                w1.Show();
                w0.Hide();
            }
        }

If you set breakpoints in the MainWindow and AnotherWindow constructors you will see they are not called once there are two windows and we SwitchHide between them.

Sure, we have more code now, but nothing difficult, just some demo code to show the principle.

Next time I hope to cover the swapping of controls in a view, we’ll see.

Advertisements

One response to “WPF Window (or do I mean View) Navigation (or do I mean switching, opening, closing…) part 2 #wpf

  1. Pingback: WPF Window (or do I mean View) Navigation (or do I mean switching, opening, closing…) part 3 #wpf | Tim's cycling blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s