如何关闭在WPF一个窗口上退出键 [英] How to Close a Window in WPF on a escape key

查看:210
本文介绍了如何关闭在WPF一个窗口上退出键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
如何分配项目中的关于取消 - 按键关闭的行为给所有WPF窗口?






我要关闭的窗户,我的WPF项目,当用户点击逃生按钮。我不想把代码写在每一个窗口,但要创建一个类可以赶上当用户按退出键。


解决方案

  ///<总结> 
///附加的行为,保持在屏幕上
///<窗口; /总结>
公共静态类WindowService
{
///<总结>
/// KeepOnScreen附加依赖项属性
///< /总结>
公共静态只读的DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(
EscapeClosesWindow,
typeof运算(布尔),
typeof运算(WindowService),
新FrameworkPropertyMetadata(假的,新的PropertyChangedCallback(OnEscapeClosesWindowChanged)));

///<总结>
///获取EscapeClosesWindow财产。这依赖属性
///表示转义键是否关闭窗口。
///< /总结>
///< PARAM NAME =D><见CREF =DependencyObject的/>获得来自以下的财产; /参数>
///<返回>在EscapeClosesWindow财产1所述的价值; /回报>
公共静态布尔GetEscapeClosesWindow(DependencyObject的D)
{
返回(布尔)d.GetValue(EscapeClosesWindowProperty);
}

///<总结>
///设置EscapeClosesWindow财产。这依赖属性
///表示转义键是否关闭窗口。
///< /总结>
///< PARAM NAME =D><见CREF =DependencyObject的/>设置针对<财产; /参数>
///< PARAM NAME =值>在财产和LT的值/参数>
公共静态无效SetEscapeClosesWindow(DependencyObject的D,布尔值)
{
d.SetValue(EscapeClosesWindowProperty,值);
}

///<总结>
///把手变为EscapeClosesWindow属性。
///< /总结>
///< PARAM NAME =D><见CREF =DependencyObject的/>引发该事件< /参数>
///< PARAM NAME =E>将<见CREF =DependencyPropertyChangedEventArgs/>包含事件数据< /参数>
私有静态无效OnEscapeClosesWindowChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
{
窗口的目标=(窗口)D;
如果(目标!= NULL)
{
target.PreviewKeyDown + =新System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);
}
}

///<总结>
///处理窗口
///< PreviewKeyDown事件; /总结>
///< PARAM NAME =发件人>该事件的源< /参数>
///< PARAM NAME =E>将<见CREF =KeyEventArgs/>包含事件数据< /参数>
私有静态无效Window_PreviewKeyDown(对象发件人,发送KeyEventArgs E)
{
窗口的目标=(窗口)发送;

//如果是退出键,关闭窗口
如果(e.Key == Key.Escape)
target.Close();
}
}

然后在XAML
的xmlns:LOC = CLR的命名空间:JIMS.Commands>


Possible Duplicate:
How can I assign the 'Close on Escape-key press' behavior to all WPF windows within a project?

I want to close the windows in my wpf project when the user clicks the escape button. I don't want to write the code in every window but want to create a class which can catch the when the user press the escape key.

解决方案

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

Then in XAML xmlns:loc="clr-namespace:JIMS.Commands">

这篇关于如何关闭在WPF一个窗口上退出键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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