0

I have a CheckBox in my MainWindow which acts as a toggle to open and close a another window (lets say NextWindow). Putting it simply what I have done up until now is that:

  • When CheckBox is checked, another window opens
  • When CheckBox is unchecked, it closes the opened window

What I want now is to change the CheckBox state, when the NextWindow is manually closed by the user. What do I need to do?
Here are my code:

For MainWindow.XAML

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="100" Width="100">
<Grid>
    <CheckBox Name="chk" Checked="chk_Checked_1" Unchecked="chk_Unchecked_1" Margin="20">Window</CheckBox>
</Grid>

For MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void chk_Checked_1(object sender, RoutedEventArgs e)
    {
        NextWindow nw = new NextWindow();
        nw.Show();
    }

    private void chk_Unchecked_1(object sender, RoutedEventArgs e)
    {
        var window = IsWindowOpen<Window>("Next");

        if (window != null)
        {
            window.Close();
        }           
    }
    public static T IsWindowOpen<T>(string name = null)
    where T : Window
    {
        var windows = Application.Current.Windows.OfType<T>();
        return string.IsNullOrEmpty(name) ? windows.FirstOrDefault() : windows.FirstOrDefault(w => w.Name.Equals(name));
    }
}

For NextWindow.xaml

<Window x:Class="WpfApplication2.NextWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NextWindow" Height="100" Width="100" Name="Next">
<Grid>
    <Label Margin="20">Hello</Label>
</Grid>

Skaranjit
  • 764
  • 9
  • 26
  • use closed event? https://stackoverflow.com/questions/10033551/wpf-window-closed-event-usage and https://msdn.microsoft.com/en-us/library/system.windows.window.closed(v=vs.110).aspx – Arie Feb 04 '16 at 07:28
  • So simple and elegant. Thank you @Arie – Skaranjit Feb 04 '16 at 08:10

2 Answers2

1

Ok, I followed the this link suggested in the comment and did following changes to my code:

MainWindow

private void chk_Checked_1(object sender, RoutedEventArgs e)
    {
        NextWindow nw = new NextWindow();
        nw.Closed += nw_Closed;
        nw.Show();
    }

    void nw_Closed(object sender, EventArgs e)
    {
        chk.IsChecked = false;
    }
Skaranjit
  • 764
  • 9
  • 26
0

Simply change the uncheck event handler like that:

private void chk_Unchecked_1(object sender, RoutedEventArgs e)
    {
        var window = IsWindowOpen<Window>("Next");

        if (window != null)
        {
            window.Close();
            ((CheckBox)sender).IsChecked = false;    
        }           
    }
Cem Şengezer
  • 213
  • 1
  • 12
  • This event is called when I uncheck the checkbox so, using `((CheckBox)sender).IsChecked = false; ` would be pointless. – Skaranjit Feb 04 '16 at 08:05