Manish K
Manish K

Reputation: 65

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.

Upvotes: 1

Views: 1287

Answers (2)

mm8
mm8

Reputation: 169370

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

Upvotes: 1

Sach
Sach

Reputation: 10393

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

Upvotes: 1

Related Questions