简体   繁体   中英

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). My item click event in the P's viewmodel has the following code:

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.

I've worked on a similar requirement for Winforms applicaiton.How do I go about this for a WPF application please?

Thanks.

First you'd need to declare myChildWindow outside of the click event so that it is accessible from multiple events. So,

MyGridView myChildWindow;

goes outside the click event, probably as a private variable.

Then, in your click event see if it's null , and if it is, create it.

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;
}

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