C# WPF 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11499932/
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-08-09 18:05:12  来源:igfitidea点击:

WPF popup window

c#wpf

提问by Joe SHI

I would like to let the user choose their options after a button is clicked. For example, showing two buttons, "Restart Now" and "Restart Later" in a modal popup window would be my preference. Using a MessageBoxis not a solution for me as it does not allow the user to change the title of buttons.

我想让用户在单击按钮后选择他们的选项。例如,在模态弹出窗口中显示两个按钮“立即重启”和“稍后重启”将是我的偏好。使用 aMessageBox对我来说不是解决方案,因为它不允许用户更改按钮的标题。

采纳答案by Botz3000

You need to create a new Window class. You can design that then any way you want. You can create and show a window modally like this:

您需要创建一个新的 Window 类。你可以按照你想要的任何方式设计它。您可以像这样以模态方式创建和显示窗口:

MyWindow popup = new MyWindow();
popup.ShowDialog();

You can add a custom property for your result value, or if you only have two possible results ( + possibly undeterminate, which would be null), you can set the window's DialogResultproperty before closing it and then check for it (it is the value returned by ShowDialog()).

您可以为结果值添加自定义属性,或者如果您只有两个可能的结果(+ 可能不确定,这将是null),您可以DialogResult在关闭窗口之前设置窗口的属性,然后检查它(它是由ShowDialog())。

回答by Harry

Simply show a new window with two buttons. Add property to contain user result.

只需显示一个带有两个按钮的新窗口。添加属性以包含用户结果。

回答by Nickon

In WPF there is a control named Popup.

在 WPF 中有一个名为 Popup 的控件。

Popup myPopup = new Popup();
//(...)
myPopup.IsOpen = true;

回答by Karthik Krishna Baiju

XAML

XAML

<Popup Name="myPopup">
      <TextBlock Name="myPopupText" 
                 Background="LightBlue" 
                 Foreground="Blue">
        Popup Text
      </TextBlock>
</Popup>

c#

C#

    Popup codePopup = new Popup();
    TextBlock popupText = new TextBlock();
    popupText.Text = "Popup Text";
    popupText.Background = Brushes.LightBlue;
    popupText.Foreground = Brushes.Blue;
    codePopup.Child = popupText;

you can find more details about the Popup Control from MSDN documentation.

您可以从 MSDN 文档中找到有关弹出控件的更多详细信息。

MSDN documentation on Popup control

关于弹出控件的 MSDN 文档