简体   繁体   English

WPF应用程序中的弹出窗口

[英]Pop-up window in a WPF application

I've a C# WPF application which show uses Grid control in the xaml(P screen).For every row in the grid, I've a column called Details.Clicking on item in this column shows a pop-up windows which also has a grid in the xaml(C screen). 我有一个C#WPF应用程序,该应用程序显示在xaml(P屏幕)中使用Grid控件。对于网格中的每一行,我都有一个名为Details的列。单击此列中的项目会显示一个弹出窗口,其中也包含xaml(C屏幕)中的网格。 My item click event in the P's viewmodel has the following code: P的视图模型中的我的项目单击事件具有以下代码:

var myChildWindow = new MyGridView();
myChildWindow.Show();

If the user clicks on the item multiple times, I just want to highlight the existing C pop-up window.If there's no existing windows open, then only I want to open a new windows. 如果用户多次单击该项目,我只想突出显示现有的C弹出窗口。如果没有现有的窗口打开,那么只有我想打开一个新窗口。

I've worked on a similar requirement for Winforms applicaiton.How do I go about this for a WPF application please? 我已经为Winforms应用程序满足了类似的要求,请问如何为WPF应用程序解决呢?

Thanks. 谢谢。

First you'd need to declare myChildWindow outside of the click event so that it is accessible from multiple events. 首先,您需要在click事件之外声明myChildWindow ,以便可以从多个事件访问它。 So, 所以,

MyGridView myChildWindow;

goes outside the click event, probably as a private variable. 超出了click事件,可能是私有变量。

Then, in your click event see if it's null , and if it is, create it. 然后,在您的click事件中查看它是否为null ,如果是,则创建它。

if (myChildWindow == null)
{
    myChildWindow = new MyGridView();
    myChildWindow.Show();
}

You could keep a reference to the window and get rid of this when the window is closed: 您可以保留对窗口的引用,并在关闭窗口时删除该引用:

MyGridView  myChildWindow;
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (myChildWindow == null)
    {
        myChildWindow = new MyGridView();
        myChildWindow.Closed += MyChildWindow_Closed;
        myChildWindow.Show();
    }
    else
    {
        myChildWindow.Activate();
    }
}

private void MyChildWindow_Closed(object sender, EventArgs e)
{
    myChildWindow.Closed -= MyChildWindow_Closed;
    myChildWindow = null;
}

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

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