Hiding a WPF Window Other Than MainWindow

Skysmith 146 Reputation points
2022-12-21T15:54:55.69+00:00

I have a WPF application with 3 windows: MainWindow, SubWindow1, and SubWindow2.

I'd like to be able to show and hide those windows.

I can hide and show MainWindow from either SubWindow1 or SubWindow2 by using Application.Current.MainWindow.Show/Hide();, but I cannot figure out how to hide SubWindow1 from SubWindow2 or vice versa.

How do I do this?

Developer technologies Windows Presentation Foundation
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-12-21T16:54:59.353+00:00

    Hi,
    try following demo:

    <Application x:Class="Application"  
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                 xmlns:local="clr-namespace:WpfApp1"  
                 StartupUri="MainWindow.xaml">  
      <Application.Resources>  
        <local:ViewModel x:Key="vm"/>  
      </Application.Resources>  
    </Application>  
      
      
    <Window x:Class="MainWindow"  
            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/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
      <StackPanel DataContext="{Binding Source={StaticResource vm}}">  
        <Button Content="SubWindow1" Command="{Binding}" CommandParameter="SubWindow1" Margin="10"/>  
        <Button Content="SubWindow2" Command="{Binding}" CommandParameter="SubWindow2" Margin="10"/>  
      </StackPanel>  
    </Window>  
      
      
    <Window x:Class="SubWindow1"  
            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/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            mc:Ignorable="d"  
            Title="SubWindow1" Height="450" Width="800">  
      <StackPanel DataContext="{Binding Source={StaticResource vm}}">  
        <Button Content="SubWindow1" Command="{Binding}" CommandParameter="SubWindow1" Margin="10"/>  
        <Button Content="SubWindow2" Command="{Binding}" CommandParameter="SubWindow2" Margin="10"/>  
      </StackPanel>  
    </Window>  
      
      
    <Window x:Class="SubWindow2"  
            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/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            mc:Ignorable="d"  
            Title="SubWindow2" Height="450" Width="800">  
      <StackPanel DataContext="{Binding Source={StaticResource vm}}">  
        <Button Content="SubWindow1" Command="{Binding}" CommandParameter="SubWindow1" Margin="10"/>  
        <Button Content="SubWindow2" Command="{Binding}" CommandParameter="SubWindow2" Margin="10"/>  
      </StackPanel>  
    </Window>  
      
      
    Public Class ViewModel  
      Implements ICommand  
      
      Private _wnds As Dictionary(Of String, Window)  
      Private ReadOnly Property Wnds As Dictionary(Of String, Window)  
        Get  
          If Me._wnds Is Nothing Then  
            _wnds = New Dictionary(Of String, Window)  
            _wnds.Add("MainWindow", Application.Current.MainWindow)  
            _wnds.Add("SubWindow1", New SubWindow1)  
            _wnds.Add("SubWindow2", New SubWindow2)  
          End If  
          Return Me._wnds  
        End Get  
      End Property  
      
      Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged  
      Public Sub Execute(parameter As Object) Implements ICommand.Execute  
        Dim wnd As Window = wnds(parameter.ToString)  
        If wnd IsNot Nothing Then  
          If wnd.Visibility = Visibility.Visible Then wnd.Hide() Else wnd.Show()  
        End If  
      End Sub  
      Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute  
        Return True  
      End Function  
      
      Protected Overrides Sub Finalize()  
        For Each kvp In Me._wnds  
          kvp.Value.Close()  
        Next  
      End Sub  
      
    End Class  
      
      
    

    Result:

    272954-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.