简体   繁体   中英

how to close a window from an open c# wpf page

I tried using the window closing method, but nothing came out

  `foreach (Window window in Application.Current.Windows)
{
  if (window.DataContext == this)
    CloseWin(window);
}


 public void CloseWin(object obj)
 {
     Window win = obj as Window;
     win.Close();
 }`

You can use this to close the current window

public void CloseWin()
{
    this.Close();
}

If you want to close the windows by DataContext,

foreach (Window window in Application.Current.Windows)
{
    if (window.DataContext == this)
    {
        window.Close();
    }
}

Updated:

private void btnClose_Click(object sender, RoutedEventArgs e)
{
    var window = Window.GetWindow((Button)sender);
    window.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM