WPF:单击按钮打开新窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15731400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:24:25  来源:igfitidea点击:

WPF: Opening new window on button click

c#wpfxamlmvvm

提问by Marek M.

Is it possible to open a new window when a button is clicked without touching the CS code? I'd like to do something like this:

是否可以在不触摸 CS 代码的情况下单击按钮时打开一个新窗口?我想做这样的事情:

<Button Command="OpenNewWindow" Content="New window" />

or

或者

<Button Command="Open NameOfMyWindow" Content="New window" />

I browsed through predefined commands, and found one called Open, though it doesn't seem to be designed for this task.

我浏览了预定义的命令,发现了一个名为 Open 的命令,尽管它似乎不是为此任务设计的。

采纳答案by Sten Petrov

You can use CommandParameter

您可以使用命令参数

<Button Command="OpenWindow" CommandParameter="NameOfMyWindow" Content="New window" />

And use that parameter in the code that handles OpenWindow command.

并在处理 OpenWindow 命令的代码中使用该参数。

The generic commands are more closely associated with the typical "File" menu, you'd be opening a File or something like that. You can write a custom command for your app

通用命令与典型的“文件”菜单更密切相关,您将打开文件或类似的东西。您可以为您的应用编写自定义命令