Keep WPF popup window open after user has made choice

Viewed 149

I have a small pop up window coming up when user selects a record from the main grid. I would like to keep this window open after user has made the selection until he or she clicks cancel. Here is my little code. If I comment out this.close() method then control never returns back to execute rest of the code.

private void ClickSubmit(object sender, RoutedEventArgs e)
{
    try
    {
        IsCancelled = false;
        // Figure out to keep this little window open after clicking submit !!
        this.Close();
        return;
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}

private void ClickCancel(object sender, RoutedEventArgs e)
{
    try
    {
        Trace.WriteLine("Popup exit !");
        IsCancelled = true;
        this.Close();
        return;
    }
    catch (Exception ex)
    {
        ex.ToString();
    }

}

OrdRec or = lvOrders.SelectedItem as OrdRec;

PxQtyPopup pop = new PxQtyPopup(or);
//pop.ShowDialog();
pop.Show();

if (!pop.IsCancelled) // check whether price/qty change submitted or popup window was cancelled
    (DCtxt as OrdViewModel).ModifyOrder(or, int.Parse(pop.Qty), decimal.Parse(pop.Px));

2 Answers

Is your Popup a new WPF window? - You answered yes.

One way to do this would be to expose 2 events in your Popup window. ExitWithClose & ExitWithoutClose. Main window can handle these 2 events and do the after popup processing in the handlers.


Otherwise, prefer using WPF Popup control. WPF Popup works with IsOpen property. Here's how to keep a popup open or close it.

You will see

Xaml:

<Grid Name="MainGrid">
    <Button Height="20" Width="100" Click="Button_Click">Click Me!</Button>
    <Popup Margin="10,10,0,13" Name="Popup1" HorizontalAlignment="Center" VerticalAlignment="Center" 
             PlacementTarget="{Binding MainGrid}" Placement="Center"
             Width="194" Height="200">
        <StackPanel Orientation="Vertical" Background="Gray">
            <Button Height="20" Width="150" Click="Button_ClickToStayOpen" Margin="10">Don't Close Me!</Button>
            <Button Height="20" Width="150" Click="Button_ClickToClose" Margin="10">Close Me!</Button>
        </StackPanel>
    </Popup>
</Grid>

Code behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
 //Show Popup
 Popup1.IsOpen = true;
}

private void Button_ClickToStayOpen(object sender, RoutedEventArgs e)
{
 // Do actions
 //Keep Popup Open
}

private void Button_ClickToClose(object sender, RoutedEventArgs e)
{
 //Close Popup
 Popup1.IsOpen = false;
}

When the window opens there will be a Click Me button. When you click it, a popup will launch. Popup will have 2 buttons. One will help you do some action without closing popup. The other will close the popup.

Sample Window Popup

Bind the IsOpen to a suitable boolean which will instead open or close the popup. The below example uses a ToggleButton's OpenState (boolean) which will open/close the popup.

<ToggleButton Height="30" Width="60"             
                x:Name="OpenState">
    <StackPanel>
        <TextBlock Text="{Binding IsChecked, ElementName=OpenState}"
                    FontSize="18"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center">
        </TextBlock>

        <Popup Name="myPopup"
                IsOpen="{Binding IsChecked, ElementName=OpenState}">


            <TextBlock Name="myPopupText"
                       Background="LightBlue"
                       Foreground="Blue">Popup Text</TextBlock>

        </Popup>

    </StackPanel>
</ToggleButton>
Related