Grid visibility when getting back to the main window
<p>I have a main window that contains a stack Panel containing some buttons. To each button I have an associated grid panel which in turn has several image buttons.  clicking a grid panel image button opens a new window . On the main window my grid visibility is set to Hidden(because I want the grid to be displayed only when one of the buttons is clicked from the stack panel)but when I click the back button on the new window, I want the  selected grid to be visible .How to achieve this? I am not using MVVM .</p>

<pre><code>&lt;Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication5.Window1"
    Title="Home_window" Height="456.163" Width="653.173" Background="#FF9CB5D3"&gt;

&lt;Window.Resources&gt;
    &lt;ResourceDictionary&gt;
        &lt;BitmapImage x:Key="buttonicon" UriSource="/Images/icon.png"/&gt;
        &lt;/ResourceDictionary&gt;
&lt;/Window.Resources&gt;



    &lt;Grid x:Name="menu" HorizontalAlignment="Left" Height="131" Margin="1,156,0,-211" VerticalAlignment="Top" Width="123"&gt;

        &lt;StackPanel x:Name="menu" HorizontalAlignment="Left" Height="150" VerticalAlignment="Top" Width="123" Margin="13,30,-13,-49"&gt;
            &lt;Button Content="Home" Height="25" Click="Button_Click_1" &gt;
                &lt;Button.Background&gt;
                    &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                        &lt;GradientStop Color="#FF3CABEA"/&gt;
                        &lt;GradientStop Color="#FFEBEBEB" Offset="0.5"/&gt;
                        &lt;GradientStop Color="#FFDDDDDD" Offset="0.322"/&gt;
                        &lt;GradientStop Color="#FF62A7CF" Offset="1"/&gt;
                    &lt;/LinearGradientBrush&gt;
                &lt;/Button.Background&gt;
            &lt;/Button&gt;
            &lt;Button Content="b" Height="25"  Click="Button_Click_8"&gt;
            &lt;Button.Background&gt;
                &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                    &lt;GradientStop Color="#FF3CABEA" Offset="0"/&gt;
                    &lt;GradientStop Color="#FFEBEBEB" Offset="0.5"/&gt;
                    &lt;GradientStop Color="#FFDDDDDD" Offset="0.322"/&gt;
                    &lt;GradientStop Color="#FF62A7CF" Offset="1"/&gt;
                &lt;/LinearGradientBrush&gt;
            &lt;/Button.Background&gt;
            &lt;/Button&gt;
            &lt;Button Content="c" Height="25"  Click="Button_Click_9"&gt;
                &lt;Button.Background&gt;
                    &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                        &lt;GradientStop Color="#FF3CABEA" Offset="0"/&gt;
                        &lt;GradientStop Color="#FFEBEBEB" Offset="0.5"/&gt;
                        &lt;GradientStop Color="#FFDDDDDD" Offset="0.322"/&gt;
                        &lt;GradientStop Color="#FF62A7CF" Offset="1"/&gt;
                    &lt;/LinearGradientBrush&gt;
                &lt;/Button.Background&gt;
            &lt;/Button&gt;

            &lt;Button Content="d" Height="25"  Click="Button_Click_10"&gt;
                &lt;Button.Background&gt;
                    &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                        &lt;GradientStop Color="#FF3CABEA" Offset="0"/&gt;
                        &lt;GradientStop Color="#FFEBEBEB" Offset="0.5"/&gt;
                        &lt;GradientStop Color="#FFDDDDDD" Offset="0.322"/&gt;
                        &lt;GradientStop Color="#FF62A7CF" Offset="1"/&gt;
                    &lt;/LinearGradientBrush&gt;
                &lt;/Button.Background&gt;
            &lt;/Button&gt;
            &lt;Button Content="e" Height="25" Click="Button_Click_11"&gt;
                &lt;Button.Background&gt;
                    &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                        &lt;GradientStop Color="#FF3CABEA" Offset="0"/&gt;
                        &lt;GradientStop Color="#FFEBEBEB" Offset="0.5"/&gt;
                        &lt;GradientStop Color="#FFDDDDDD" Offset="0.322"/&gt;
                        &lt;GradientStop Color="#FF62A7CF" Offset="1"/&gt;
                    &lt;/LinearGradientBrush&gt;
                &lt;/Button.Background&gt;
            &lt;/Button&gt;

     &lt;/StackPanel&gt;

    &lt;/Grid&gt;



    &lt;Grid x:Name="grid_home" HorizontalAlignment="Left" Height="260" Margin="167,156,-123,-105" VerticalAlignment="Top" Width="445" Background="#FF577EAC" Visibility="Hidden"&gt;
        &lt;Button Margin="35,29,305,160" Click="Button_Click_2"&gt;
            &lt;StackPanel x:Name="inventory" Orientation="Horizontal" Height="66"&gt;
                &lt;Image Source="images/inventory.png" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,12,0,10"/&gt;

                &lt;TextBlock Text="  Inventory" FontSize="12" Width="67" Height="19" Margin="0,24,0,23"/&gt;
            &lt;/StackPanel&gt;
        &lt;/Button&gt;

        &lt;Button Margin="167,29,173,160" Click="Button_Click_3" &gt;
            &lt;StackPanel x:Name="supplier" Orientation="Horizontal" Height="66"&gt;
                &lt;Image Source="images/supplier.jpg" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,12,0,10"/&gt;

                &lt;TextBlock Text="  Supplier" FontSize="12" Width="67" Height="19" Margin="0,24,0,23"/&gt;
            &lt;/StackPanel&gt;
        &lt;/Button&gt;
        &lt;Button Margin="302,29,38,160" Click="Button_Click_4"&gt;
            &lt;StackPanel x:Name="purchase order" Orientation="Horizontal" Height="66"&gt;
                &lt;Image Source="images/po.jpg" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,9,0,13"/&gt;

                &lt;TextBlock FontSize="12" Width="68" Margin="0,17,0,15"&gt;&lt;Run Text="  Purchase"/&gt;&lt;LineBreak/&gt;&lt;Run Text="     Order"/&gt;&lt;/TextBlock&gt;
            &lt;/StackPanel&gt;
        &lt;/Button&gt;
        &lt;Button Margin="35,141,305,49" Click="Button_Click_5"&gt;
            &lt;StackPanel  x:Name="shipping" Orientation="Horizontal" Height="66" Width="101"&gt;
                &lt;Image Source="images/shipping.png" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,6,0,16"/&gt;

                &lt;TextBlock FontSize="12" Width="68" Margin="0,17"&gt;&lt;Run Text=" Shipping "/&gt;&lt;/TextBlock&gt;
            &lt;/StackPanel&gt;

        &lt;/Button&gt;

        &lt;Button  Margin="166,142,174,48" Click="Button_Click_6" &gt;
            &lt;StackPanel x:Name="sales" Orientation="Horizontal" Height="66"&gt;
                &lt;Image Source="images/sales.jpg
                       " Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,7,0,15"/&gt;

                &lt;TextBlock FontSize="12" Width="68" Margin="0,20,0,14" Text="    Sales "/&gt;
            &lt;/StackPanel&gt;
        &lt;/Button&gt;
        &lt;Button Margin="303,143,38,47" Click="Button_Click_7" &gt;
            &lt;StackPanel x:Name="purchase report" Orientation="Horizontal" Height="66"&gt;
                &lt;Image Source="images/pr.jpg" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,8,0,14"/&gt;

                &lt;TextBlock FontSize="12" Width="68" Margin="0,14,0,17"&gt;&lt;Run Text=" Purchase "/&gt;&lt;LineBreak/&gt;&lt;Run Text="  Reports"/&gt;&lt;/TextBlock&gt;
            &lt;/StackPanel&gt;
        &lt;/Button&gt;

    &lt;/Grid&gt;
    &lt;Grid x:Name="grid_b" HorizontalAlignment="Left" Height="260" Margin="167,156,-123,-105" VerticalAlignment="Top" Width="445" Background="#FFB5CDEA" Visibility="Hidden"&gt;
    &lt;/Grid&gt;
</code></pre>

<p></p>

<p>On selecting Home option it displays grid_home. On grid_home on clicking inventory button it opens inventory window:</p>

<pre><code>  &lt;Window x:Class="WpfApplication5.inventory"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Inventory" Height="300" Width="469.611"&gt;
&lt;Grid Margin="0,0,2,0"&gt;
    &lt;Button Content="Back" HorizontalAlignment="Left" Margin="195,223,0,0" VerticalAlignment="Top" Width="75" Height="37" Click="Button_Click_1"/&gt;
</code></pre>

<p>
</p>

<p>part of codebehind for home window:</p>

<pre><code>   private void Button_Click_1(object sender, RoutedEventArgs e)
    {

        grid_home.Visibility = Visibility.Visible;
        grid_b.Visibility = Visibility.Collapsed;

    }

    private void Button_Click_8(object sender, RoutedEventArgs e)
    {
        inventory obj = new inventory();
        obj.Show();
        this.Close();

    }
</code></pre>

<hr>

<p>part of  code behind for inventory window:</p>

<pre><code>     private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Home_window obj = new Home_window();
        obj.Show();
        this.Close();
        grid_home.Visibility = Visibility.Visible;
    }
}
</code></pre>

<p>}</p>