WPF 取消按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16142626/
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
WPF Cancel Button
提问by Samarth Agarwal
I am working on a WPF Application. One of my Windows has a Button called "Cancel" with its IsCancel=true. I need to show a message box with Yes/No when the user clicks Cancel or presses ESCAPEKey. If the user click Yes, the Window should continue closing but if the user clicks No, it should not close the form but continue the regular operation with the Window opened. How can I do so? Please help. Thanks in advance.
我正在开发 WPF 应用程序。我的一个 Windows 有一个名为“取消”的按钮,其IsCancel=true. 当用户单击取消或ESCAPE按键时,我需要显示一个带有 Yes/No 的消息框。如果用户单击是,窗口应继续关闭,但如果用户单击否,则不应关闭窗体,而是在窗口打开的情况下继续常规操作。我该怎么做?请帮忙。提前致谢。
回答by Chamika Sandamal
this will help you
这会帮助你
void Window_Closing(object sender, CancelEventArgs e)
{
MessageBoxResult result = MessageBox.Show(
"msg",
"title",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.No)
{
// If user doesn't want to close, cancel closure
e.Cancel = true;
}
}
回答by JleruOHeP
回答by Akshay Joy
var Ok = MessageBox.Show("Are you want to Close", "WPF Application", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (Ok == MessageBoxResult.Yes)
{
this.Close();
}
else
{
}
回答by Hjalmar Z
Open a messagebox and read the result like so:
打开一个消息框并像这样读取结果:
DialogResult result = MessageBox.Show(
"Text",
"Title",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//The user clicked 'Yes'
}
else if (result == DialogResult.No)
{
//The user clicked 'No'
}
else
{
//If the user somehow didn't click 'Yes' or 'No'
}

