如何在WPF Button.IsCancel性质的工​​作? [英] How does the WPF Button.IsCancel property work?

查看:470
本文介绍了如何在WPF Button.IsCancel性质的工​​作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个取消按钮背后的基本思想是使与Escape键preSS关闭你的窗口。

The basic idea behind a Cancel button is to enable closing your window with an Escape Keypress.

您可以设置在IsCancel财产
  取消按钮为true,导致
  取消按钮自动关闭
  不处理单击对话框
  事件。

You can set the IsCancel property on the Cancel button to true, causing the Cancel button to automatically close the dialog without handling the Click event.

来源:编程WPF(格里菲斯,销售)

Source: Programming WPF (Griffith, Sells)

所以这个应该工作

<Window>
<Button Name="btnCancel" IsCancel="True">_Close</Button>
</Window>

不过,我希望的行为不工作了我。父窗口是由Application.StartupUri属性指定的主应用程序窗口。什么工作原理是

However the behavior I expect isn't working out for me. The parent window is the main application window specified by the Application.StartupUri property. What works is

<Button Name="btnCancel" IsCancel=True" Click="CloseWindow">_Close</Button>

private void CloseWindow(object sender, RoutedEventArgs) 
{
    this.Close();
}


  • 是基于IsCancel不同的行为对窗口是否是一个正常的窗口或对话框?是否IsCancel工作,因为只有当广告的ShowDialog被称为?

  • 是按钮需要(与IsCancel设置为true)到一个转义preSS关闭一个窗口一个明确的Click处理程序?

  • 推荐答案

    是的,它只能在对话框作为一个正常的窗口没有取消的概念,这是一样的DialogResult.Cancel从返回的ShowDialog中的WinForms。

    Yes, it only works on dialogs as a normal window has no concept of "cancelling", it's the same as DialogResult.Cancel returning from ShowDialog in WinForms.

    如果你想关闭一个窗口逃生,你可以在它是否是一个Key.Escape处理程序添加到previewKeyDown在窗口上,皮卡和关闭形式:

    If you wanted to close a Window with escape you could add a handler to PreviewKeyDown on the window, pickup on whether it is Key.Escape and close the form:

    public MainWindow()
    {
        InitializeComponent();
    
        this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);
    }
    
    private void CloseOnEscape(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Escape)
            Close();
    }
    

    这篇关于如何在WPF Button.IsCancel性质的工​​作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆