简体   繁体   中英

Can't close Window in WPF C#

Here's my problem : I have my first WPF window, and I create a second when changing the value of the SelectedItem of a Combobox. And I want to close the window I created when the value is changed again. I tried this :

var cCEntityWindow = new Windows.CCEntityWindow(dptList);
cCEntityWindow.CloseWindow();

from the codebehind of my first Window, but it doesn't work, so I create a simple method in my second Window :

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

and I call it from my first Window, but it doesn't work either, and I don't know why !

How should I do this ?

You should call this.Close() from the Window that you want to close, not from the other one. You could try this method, by passing the windowName

Window wintoclose = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.Name == "Window Name");
wintoclose.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