繁体   English   中英

WPF Button.IsCancel属性如何工作?

[英]How does the WPF Button.IsCancel property work?

“取消”按钮背后的基本思想是使用Escape Keypress关闭窗口。

您可以将“取消”按钮上的IsCancel属性设置为true,从而使“取消”按钮自动关闭对话框而不处理Click事件。

资料来源:编程WPF(格里菲斯,卖出)

所以这应该工作

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

然而,我期望的行为并不适合我。 父窗口是Application.StartupUri属性指定的主应用程序窗口。 有效的是

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

private void CloseWindow(object sender, RoutedEventArgs) 
{
    this.Close();
}
  • 根据窗口是普通窗口还是对话框,IsCancel的行为是否不同? 只有在调用ShowDialog时,IsCancel才能像宣传的那样工作吗?
  • 按钮是否需要显式的Click处理程序(IsCancel设置为true)以关闭Escape印刷机上的窗口?

是的,它只适用于对话框,因为普通窗口没有“取消”的概念,它与从WinForms中的ShowDialog返回的DialogResult.Cancel相同。

如果你想关闭一个带有转义的Window,你可以在窗口上为PreviewKeyDown添加一个处理程序,拾取它是否是Key.Escape并关闭表单:

public MainWindow()
{
    InitializeComponent();

    this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);
}

private void CloseOnEscape(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Escape)
        Close();
}

我们可以进一步采取史蒂夫的答案并创建一个附加属性,为任何窗口提供“关闭时逃逸”功能。 写一次属性并在任何窗口中使用它。 只需将以下内容添加到窗口XAML:

yournamespace:WindowService.EscapeClosesWindow="True"

这是该属性的代码:

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

这是不对的... MSDN说:当您将按钮的IsCancel属性设置为true时,您将创建一个使用AccessKeyManager注册的Button。 然后,当用户按下ESC键时,该按钮被激活。 因此,您需要在代码中使用处理程序而且您不需要任何附加属性或类似的东西

是的,这是正确的。在WPF中的Windows应用程序中,AcceptButton和Cancel Button就在那里。 但有一点是,如果您将控件可见性设置为false,则它将无法按预期工作。为此,您需要在WPF中将可见性视为true。 例如:(它不适用于取消按钮,因为此处可见性为假)

<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click" Visibility="Hidden"></Button> 

所以,你需要做到:

<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click"></Button>

然后你在文件后面的代码中写了btnClose_Click

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM