简体   繁体   English

无法在WPF C#中关闭窗口

[英]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. 这是我的问题:我有第一个WPF窗口,并且在更改组合框的SelectedItem的值时创建了第二个窗口。 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 : 从我的第一个Window的代码后面开始,但是它不起作用,所以我在第二个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 ! 我从第一个Window调用它,但是它也不起作用,我也不知道为什么!

How should I do this ? 我该怎么办?

You should call this.Close() from the Window that you want to close, not from the other one. 您应该从要关闭的窗口而不是另一个窗口中调用this.Close() You could try this method, by passing the windowName 您可以通过传递windowName来尝试此方法

Window wintoclose = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.Name == "Window Name");
wintoclose.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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