打开弹出窗口并尝试关闭主窗口时,仅关闭弹出窗口而不关闭主窗口

时间:2018-08-14 05:17:58

标签: c# wpf popup

我有一个名为“书签”的按钮。为了显示书签,我使用了弹出窗口。当我单击名为“书签”的按钮时,将打开弹出窗口,如果单击任何其他按钮,请说,主窗口中的“播放”应播放视频并关闭弹出窗口。这一切工作正常,但问题是===>当我尝试最小化或关闭主窗口时,它仅关闭弹出窗口而不关闭主窗口。简而言之,在弹出窗口打开时,我需要点击两次关闭按钮以关闭主窗口。

我使用的代码示例

public void Show(object viewModel, string title, double width, double height)
    { 
        m_popup = new Popup();
        m_popup.AllowsTransparency = true;
        m_popup.StaysOpen = false;
        m_popup.IsOpen = true;
        m_popup.PlacementRectangle = new Rect(App.Current.MainWindow.Left + 570, App.Current.MainWindow.Top + 60, 0, 0);
        m_popup.VerticalOffset = 20;
        ContentControl contentControl = new ContentControl();
        contentControl.Content = viewModel;
        m_popup.Child = contentControl;
        m_popup.Closed += Popup_Closed;
        IsActive = true;
    }

private void Popup_Closed(object sender, EventArgs e)
{
    IsActive = false;
}

注意:请忽略IsActive属性。我已将其用于其他目的。

我曾尝试通过将主窗口设置为所有者来尝试模式对话框,但这没有用,因为我希望在打开弹出窗口时主窗口是交互式的。请给我建议解决方案。

谢谢.....

1 个答案:

答案 0 :(得分:0)

您似乎需要非模式子窗口而不是弹出窗口:

public partial class MainWindow : Window
{
    // FloatingWindow is just a window descendant
    private FloatingWindow floatingWindow;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (floatingWindow == null)
        {
            floatingWindow = new FloatingWindow();
            floatingWindow.Owner = this;
            floatingWindow.Closed += FloatingWindow_Closed;
        }

        floatingWindow.Show();
        floatingWindow.Activate();
    }

    private void FloatingWindow_Closed(object sender, EventArgs e)
    {
        floatingWindow = null;
    }
}