9

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

我在这里找到了答案:WPF 和 ShowDialog()但它不适合我,因为我不需要“始终在顶部”工具窗口。

提前致谢

4

7 回答 7

12

尝试设置Owner对话框的属性。那应该行得通。

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

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

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
    }
}
于 2012-03-24T22:05:22.970 回答
2

我遇到了这个问题,但是当从视图模型打开窗口时,我没有对当前窗口的引用。为了绕过它,我使用了以下代码:

var myWindow = new MyWindowType();
myWindow.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

您可以使用:myWindow.Owner = Application.Current.MainWindow;

但是,如果您像这样打开三个窗口,则此方法会导致问题:

MainWindow
   |
   -----> ChildWindow1

               |
               ----->  ChildWindow2

然后设置 ChildWindow2.Owner = Application.Current.MainWindow 会将窗口的所有者设置为其祖父窗口,而不是父窗口。

于 2014-03-07T11:03:00.220 回答
1

当父窗口创建(并显示)子窗口时,您需要设置所有者。

public partial class MainWindow : Window
{

    private void openChild()
    {
        ChildWindow child = new ChildWindow ();
        child.Owner = this; // "this" is the parent
        child.ShowDialog();
    }
 }

此外,如果您不想为所有孩子提供额外的任务栏......那么

<Window x:Class="ChildWindow"           
        ShowInTaskbar="False" >
</Window>
于 2012-04-06T15:54:47.143 回答
1

MVVM 模式的大部分原因是您的交互逻辑可以进行单元测试。出于这个原因,你永远不应该直接从 ViewModel 打开一个窗口,否则你会在单元测试的中间弹出对话框。

相反,您应该引发 View 将处理的事件并为您打开一个对话框。例如,请参阅这篇关于交互请求的文章:https ://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx#sec12

于 2016-05-02T21:50:35.303 回答
0

添加“ShowInTaskbar”并将其设置为 false。

于 2012-03-25T02:13:30.257 回答
0

即使这篇文章有点旧,我希望我可以发布我的解决方案。以上所有结果我都知道,并没有完全产生预期的结果。

我正在为其他谷歌员工做这件事:)

假设 f2 是您要在 f1 之上显示的窗口:

f2.Owner = Window.GetWindow(this);
f2.ShowDialog();

就是这样,我保证它不会消失!

HTH 家伙

于 2015-07-15T15:14:32.933 回答
0

这个问题似乎Window.OwnerWPF 那不是它。微软不断改变事物以保持事物的趣味性。

In WPF you can have a dialog with a specific owner and you can still have the dialog appear in the taskbar. Because why not. And that's the default behavior. Because why not. Their rationale is that modal dialogs are not kosher anymore, so you should not be using them; you should be using modeless dialogs, which make sense to show as separate taskbar icons, and in any case the user can then decide whether they want to see different app windows as separate icons, or whether they want to see them grouped.

So, they are trying to enforce this policy with complete disregard to anyone who might want to go against their guidelines and create a modal dialog. So, they force you to explicitly state that you do not want a taskbar icon to appear for your dialog.

To fix this problem, do the following in the constructor of your view class:

ShowInTaskbar = false;

(This may happen right after InitializeComponent();

This is equivalent to Xcalibur37's answer, though the way I figure things, since WPF forces you to have both a .cs file and a .xaml file, you might as well put things that are unlikely to change in the .cs file.

于 2020-02-10T17:37:30.707 回答