【问题标题】:WPF Cancel ButtonWPF 取消按钮
【发布时间】:2013-04-22 08:32:40
【问题描述】:

我正在开发 WPF 应用程序。我的一个 Windows 有一个名为“取消”的按钮,其 IsCancel=true。当用户单击取消或按ESCAPE 键时,我需要显示一个带有是/否的消息框。如果用户单击“是”,窗口应继续关闭,但如果用户单击“否”,则不应关闭表单,而是在打开窗口的情况下继续常规操作。我该怎么做?请帮忙。提前致谢。

【问题讨论】:

    标签: c# wpf visual-studio-2010 button


    【解决方案1】:

    这对你有帮助

    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;
            }        
    }
    

    【讨论】:

      【解决方案2】:

      您可以在WindowClosingenent 处理。

      看看here。有一个非常接近你的例子。

      【讨论】:

        【解决方案3】:
          var Ok = MessageBox.Show("Are you want to Close", "WPF Application", MessageBoxButton.YesNo, MessageBoxImage.Information);
        
                    if (Ok == MessageBoxResult.Yes)
                    {
                        this.Close();
                    }
                    else
                    {
        
                    }
        

        【讨论】:

        • 这就是问题所在! No怎么办?
        • 在 NO 部分中使用e.Cancel=True 做到了。
        【解决方案4】:

        打开一个消息框并像这样读取结果:

        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'
        }
        

        【讨论】:

          猜你喜欢
          • 2012-12-10
          • 2010-12-02
          • 2015-12-19
          • 1970-01-01
          • 2010-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多