简体   繁体   中英

WPF open popup window and work on the main window while the popup is open

I'm new to WPF. In my application I have to show a chart in a popup window when the show chart button is clicked in the main window. While the popup is open and displaying, the user should be able to change the values in the main window so that the chart will automatically updates.

The issue is, how can I display the popup while the user work in the main window. The Window.ShowDialog() will not allow to work in the main window while the popup is open. And when I use the Window.Show(), when the focus is set to mainwindow, the popup goes to the back and the chart is not displayed.

How can I enable the user to work in the main window while the popup is displaying the content.

Thanks in advance

The popup shouldn't be opened with Window.ShowDialog() , since that would make it modal (and you would not be able to return to the main window until it is closed.)

If you open the popup window with Window.Show() , you can tell the popup to stay on top of everything by setting TopMost to true on it.

var popup = new PopupWindow();
popup.Show();
popup.TopMost = true;

you could even pull back the focus to the main window afterward by calling .Activate() on that window.

You could nest your chart in a Popup rather than in a Window and then launch it like this

<Button x:Name="button" Click="button_Click"/>
<Popup x:Name="popup" StaysOpen="true">
    <TextBlock Text="Clicked"/>
</Popup>

private void button_Click(object sender, RoutedEventArgs e)
{
    if (!popup.IsOpen) popup.IsOpen = true; // Open it if it's not open
    else popup.IsOpen = false; // Close it if it's open
}

This article describes more of your options.

Alternatively, you could try starting your Windows in separate UI threads like this article suggests. Something like this

<Button x:Name="button" Click="button_Click"/>

private void button_click(object sender, RoutedEventArgs e)
{
    Thread thread = new Thread(() =>
    {
        MyWindow w = new MyWindow();
        w.Show();

        w.Closed += (sender2, e2) =>
        w.Dispatcher.InvokeShutdown();

        System.Windows.Threading.Dispatcher.Run();
    });

    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

The advantage of the popup approach is it's much simpler. One advantage of the multiple UI thread approach is that you could have a second, fully functioning, window not a popup.

An issue with both approaches is that when you move your main window your second window won't move with it. This being said, at least with the second window approach it's clearer that your second window is independent of your first than it is with the popup approach. Solutions to this problem include using an adorner as suggested in this question or setting aside a portion of your main window for "additional information" and then you could show/hide things like your chart by changing the visibility.

if you show the popup on a separate thread it will not stop block your main window eg

public void MessageBoxShow(string Message)
{
    Thread errorMessageThread = new Thread(() =>
    {
        MessageBox.Show(Message);
    });
    errorMessageThread.IsBackground = true;
    errorMessageThread.Start();
}

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