WPF Button.IsCancel 属性如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/419596/
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
How does the WPF Button.IsCancel property work?
提问by Gishu
The basic idea behind a Cancel button is to enable closing your window with an Escape Keypress.
取消按钮背后的基本思想是使用 Escape Keypress 关闭窗口。
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.
您可以将取消按钮上的 IsCancel 属性设置为 true,从而使取消按钮自动关闭对话框而不处理 Click 事件。
Source: Programming WPF (Griffith, Sells)
资料来源:Programming WPF(Griffith,Sells)
So this should work
所以这应该有效
<Window>
<Button Name="btnCancel" IsCancel="True">_Close</Button>
</Window>
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
然而,我期望的行为并不适合我。父窗口是由 Application.StartupUri 属性指定的主应用程序窗口。有效的是
<Button Name="btnCancel" IsCancel=True" Click="CloseWindow">_Close</Button>
private void CloseWindow(object sender, RoutedEventArgs)
{
this.Close();
}
- Is the behavior of IsCancel different based on whether the Window is a normal window or a Dialog? Does IsCancel work as advertised only if ShowDialog has been called?
- Is an explicit Click handler required for the button (with IsCancel set to true) to close a window on an Escape press?
- 根据 Window 是普通窗口还是 Dialog,IsCancel 的行为是否有所不同?只有在调用 ShowDialog 时,IsCancel 才能像宣传的那样工作吗?
- 按钮(IsCancel 设置为 true)是否需要显式 Click 处理程序来关闭 Escape 按下的窗口?
采纳答案by Steven Robbins
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.
是的,它只适用于对话框,因为普通窗口没有“取消”的概念,它与 WinForms 中从 ShowDialog 返回的 DialogResult.Cancel 相同。
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:
如果你想关闭一个带有转义的窗口,你可以在窗口上的 PreviewKeyDown 中添加一个处理程序,选择它是否是 Key.Escape 并关闭表单:
public MainWindow()
{
InitializeComponent();
this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);
}
private void CloseOnEscape(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
}
回答by John Myczek
We can take Steve's answer one step further and create an attached property that provides the "escape on close" functionality for any window. Write the property once and use it in any window. Just add the following to the window XAML:
我们可以将史蒂夫的回答更进一步,并创建一个附加属性,为任何窗口提供“关闭时转义”功能。一次写入该属性并在任何窗口中使用它。只需将以下内容添加到窗口 XAML:
yournamespace:WindowService.EscapeClosesWindow="True"
Here's the code for the property:
这是该属性的代码:
using System.Windows;
using System.Windows.Input;
/// <summary>
/// Attached behavior that keeps the window on the screen
/// </summary>
public static class WindowService
{
/// <summary>
/// KeepOnScreen Attached Dependency Property
/// </summary>
public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
"EscapeClosesWindow",
typeof(bool),
typeof(WindowService),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));
/// <summary>
/// Gets the EscapeClosesWindow property. This dependency property
/// indicates whether or not the escape key closes the window.
/// </summary>
/// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
/// <returns>The value of the EscapeClosesWindow property</returns>
public static bool GetEscapeClosesWindow(DependencyObject d)
{
return (bool)d.GetValue(EscapeClosesWindowProperty);
}
/// <summary>
/// Sets the EscapeClosesWindow property. This dependency property
/// indicates whether or not the escape key closes the window.
/// </summary>
/// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
/// <param name="value">value of the property</param>
public static void SetEscapeClosesWindow(DependencyObject d, bool value)
{
d.SetValue(EscapeClosesWindowProperty, value);
}
/// <summary>
/// Handles changes to the EscapeClosesWindow property.
/// </summary>
/// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
/// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Window target = (Window)d;
if (target != null)
{
target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
}
}
/// <summary>
/// Handle the PreviewKeyDown event on the window
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">A <see cref="KeyEventArgs"/> that contains the event data.</param>
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
Window target = (Window)sender;
// If this is the escape key, close the window
if (e.Key == Key.Escape)
target.Close();
}
}
回答by FastAndBulbous
This isn't quite right is it... MSDN says this: When you set the IsCancel property of a button to true, you create a Button that is registered with the AccessKeyManager. The button is then activated when a user presses the ESC key. So you do need a handler in your code behind And you don't need any attached properties or anything like that
这不太对吧... MSDN 是这样说的:当您将按钮的 IsCancel 属性设置为 true 时,您将创建一个注册到 AccessKeyManager 的 Button。然后当用户按下 ESC 键时激活该按钮。所以你确实需要在你的代码后面有一个处理程序而且你不需要任何附加的属性或类似的东西
回答by Pitambar
Yes this is right.In windows Application in WPF AcceptButton and Cancel Button is there. But one thing is that if you are setting your control visibility as false, then it won't work as expected.For that you need to make as visibility as true in WPF. For example: (it is not working for Cancel button because here visibility is false)
是的,这是对的。在 WPF 中的 Windows 应用程序中,AcceptButton 和 Cancel Button 就在那里。但有一点是,如果您将控件可见性设置为 false,则它不会按预期工作。为此,您需要在 WPF 中将可见性设置为 true。例如:(它不适用于取消按钮,因为这里的可见性为假)
<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click" Visibility="Hidden"></Button>
So, you need make it:
所以,你需要做到:
<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click"></Button>
Then you have write btnClose_Click
in code behind file:
然后你btnClose_Click
在文件后面写了代码:
private void btnClose_Click (object sender, RoutedEventArgs e)
{ this.Close(); }