本文介绍了ShowDialog() 在父窗口后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ShowDialog()WindowStyle = WindowStyle.SingleBorderWindow; 在我的 WPF (MVVM) 应用程序中打开一个模式窗口,但它让我导航到父级使用 Windows 任务栏的窗口 (Windows 7).

I am using ShowDialog() with WindowStyle = WindowStyle.SingleBorderWindow; to open a modal window in my WPF (MVVM) application, but it lets me navigate to parent window using the Windows taskbar (Windows 7).

我在这里找到了答案:WPF 和 ShowDialog() 但它不适合对我来说,因为我不需要始终在最前面"的工具窗口.

I've found an answer here: WPF and ShowDialog() but it isn't suitable for me because I don't need an "always on top" tool window.

提前致谢

推荐答案

尝试设置对话框的 Owner 属性.那应该可行.

Try setting the Owner property of the dialog. That should work.

Window dialog = new Window();
dialog.Owner = mainWindow;
dialog.ShowDialog();

我在使用 MVVM 时遇到了类似的问题.您可以通过使用委托来解决此问题.

I had a similar problem using this with MVVM. You can solve this by using delegates.

public class MainWindowViewModel
{
    public delegate void ShowDialogDelegate(string message);
    public ShowDialogDelegate ShowDialogCallback;

    public void Action()
    {
        // here you want to show the dialog
        ShowDialogDelegate callback = ShowDialogCallback;
        if(callback != null)
        {
            callback("Message");
        }
    }
}

public class MainWindow
{
    public MainWindow()
    {
        // initialize the ViewModel
        MainWindowViewModel viewModel = new MainWindowViewModel();
        viewModel.ShowDialogCallback += ShowDialog;
        DataContext = viewModel;
    }

    private void ShowDialog(string message)
    {
        // show the dialog
    }
}

这篇关于ShowDialog() 在父窗口后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:52