简体   繁体   English

将对话框窗口置于前面

[英]Bring dialog window to front

I am showing a window on a button click like this: 我在按钮点击上显示一个窗口,如下所示:

private void showWindow(object obj)
{
    var dialog = new AddItemView();
    dialog.Show();
}

If the button is clicked again, while this window is still open, how do I bring this window to the front and not create a new one? 如果再次单击该按钮,当此窗口仍处于打开状态时,如何将此窗口置于前面而不创建新窗口?

Just store the dialog object and check whether it's already been created in showWindow. 只需存储对话框对象并检查它是否已在showWindow中创建。

Used the windows Closed event to clear the reference to the dialog object. 使用Windows Closed事件清除对象对象的引用。

AddItemView dialog;

private void showWindow(object obj)
{

    if ( dialog == null )
    {
       dialog = new AddItemView();
       dialog.Show();
       dialog.Owner = this;
       dialog.Closed += new EventHandler(AddItemView_Closed);
    }
    else
       dialog.Activate();
}

void AddItemView_Closed(object sender, EventArgs e)
    {

        dialog = null;
    }

Just a quick sketch but this should do what you want: 只是一个快速草图,但这应该做你想要的:

Window1 W = new Window1();

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (W.IsVisible)
        W.Activate();
    else
        W.Show();
}

If this does not do it, maybe I have misread your question. 如果不这样做,也许我误解了你的问题。

Edited to correct a bug. 编辑纠正错误。

Add this on the class constructor where you are instantiating the window. 在实例化窗口的类构造函数中添加它。 A window cannot be closed after is opened. 打开后窗口无法关闭。

        W.Closing += (s, e) => 
        {
            e.Cancel = true;
            ((Window)s).Hide();
        };

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

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