【问题标题】:When popup is opened and try to close the main window, It closes only popup not main window当弹出窗口打开并尝试关闭主窗口时,它只关闭弹出窗口而不是主窗口
【发布时间】:2018-08-14 05:17:58
【问题描述】:

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

我用过的代码示例

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 属性。我已经将它用于其他目的。

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

谢谢.....

【问题讨论】:

  • 这是预期的行为以及弹出窗口的工作方式。 “窃取”焦点,因此您必须先将其关闭,然后单击任何按钮。

标签: c# wpf popup


【解决方案1】:

看起来您需要非模态子窗口而不是弹出窗口:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多