当前位置:  开发笔记 > 开发工具 > 正文

WPF Button.IsCancel属性如何工作?

如何解决《WPFButton.IsCancel属性如何工作?》经验,为你挑选了3个好方法。

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

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

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

所以这应该工作




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



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

根据窗口是普通窗口还是对话框,IsCancel的行为是否不同?只有在调用ShowDialog时,IsCancel才能像宣传的那样工作吗?

按钮是否需要显式的Click处理程序(IsCancel设置为true)才能关闭Escape印刷机上的窗口?

Steven Robbi.. 32

是的,它只适用于对话框,因为普通窗口没有"取消"的概念,它与从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();
}


John Myczek.. 17

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

yournamespace:WindowService.EscapeClosesWindow="True"

这是该属性的代码:

using System.Windows;
using System.Windows.Input;

/// 
/// Attached behavior that keeps the window on the screen
/// 
public static class WindowService
{
   /// 
   /// KeepOnScreen Attached Dependency Property
   /// 
   public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
      "EscapeClosesWindow",
      typeof(bool),
      typeof(WindowService),
      new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));

   /// 
   /// Gets the EscapeClosesWindow property.  This dependency property 
   /// indicates whether or not the escape key closes the window.
   /// 
   ///  to get the property from
   /// The value of the EscapeClosesWindow property
   public static bool GetEscapeClosesWindow(DependencyObject d)
   {
      return (bool)d.GetValue(EscapeClosesWindowProperty);
   }

   /// 
   /// Sets the EscapeClosesWindow property.  This dependency property 
   /// indicates whether or not the escape key closes the window.
   /// 
   ///  to set the property on
   /// value of the property
   public static void SetEscapeClosesWindow(DependencyObject d, bool value)
   {
      d.SetValue(EscapeClosesWindowProperty, value);
   }

   /// 
   /// Handles changes to the EscapeClosesWindow property.
   /// 
   ///  that fired the event
   /// A  that contains the event data.
   private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      Window target = (Window)d;
      if (target != null)
      {
         target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
      }
   }

   /// 
   /// Handle the PreviewKeyDown event on the window
   /// 
   /// The source of the event.
   /// A  that contains the event data.
   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();
   }
}

是.附属物仍然不会立即"点击". (3认同)


小智.. 6

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



1> Steven Robbi..:

是的,它只适用于对话框,因为普通窗口没有"取消"的概念,它与从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();
}



2> John Myczek..:

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

yournamespace:WindowService.EscapeClosesWindow="True"

这是该属性的代码:

using System.Windows;
using System.Windows.Input;

/// 
/// Attached behavior that keeps the window on the screen
/// 
public static class WindowService
{
   /// 
   /// KeepOnScreen Attached Dependency Property
   /// 
   public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
      "EscapeClosesWindow",
      typeof(bool),
      typeof(WindowService),
      new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));

   /// 
   /// Gets the EscapeClosesWindow property.  This dependency property 
   /// indicates whether or not the escape key closes the window.
   /// 
   ///  to get the property from
   /// The value of the EscapeClosesWindow property
   public static bool GetEscapeClosesWindow(DependencyObject d)
   {
      return (bool)d.GetValue(EscapeClosesWindowProperty);
   }

   /// 
   /// Sets the EscapeClosesWindow property.  This dependency property 
   /// indicates whether or not the escape key closes the window.
   /// 
   ///  to set the property on
   /// value of the property
   public static void SetEscapeClosesWindow(DependencyObject d, bool value)
   {
      d.SetValue(EscapeClosesWindowProperty, value);
   }

   /// 
   /// Handles changes to the EscapeClosesWindow property.
   /// 
   ///  that fired the event
   /// A  that contains the event data.
   private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      Window target = (Window)d;
      if (target != null)
      {
         target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
      }
   }

   /// 
   /// Handle the PreviewKeyDown event on the window
   /// 
   /// The source of the event.
   /// A  that contains the event data.
   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();
   }
}


是.附属物仍然不会立即"点击".

3> 小智..:

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


MSDN不太完整 - 请参阅此处的代码:http://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Controls/Button.cs,274按下按钮时,如果没有分配命令,设置IsCancel后,DialogCancel会自动执行(OnClick处理程序完成后).
推荐阅读
360691894_8a5c48
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有