|
// -------------------------------------------------------------------
// Member of the WPFSmartLibrary
// For more information see : (coming soon ...)
// (by DotNetMastermind)
//
// filename : WindowKeysHandling.cs
// namespace : SoftArcs.WPFSmartLibrary.UIClassAttachedProperties
// class(es) : WindowKeysHandling
//
// -------------------------------------------------------------------
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace SoftArcs.WPFSmartLibrary.UIClassAttachedProperties
{
public static class WindowKeysHandling
{
#region DependencyProperty - EscapeClosesWindow ("bool")
public static bool GetEscapeClosesWindow(DependencyObject dpo)
{
return (bool)dpo.GetValue( EscapeClosesWindowProperty );
}
public static void SetEscapeClosesWindow(DependencyObject dpo, bool value)
{
dpo.SetValue( EscapeClosesWindowProperty, value );
}
public static readonly DependencyProperty EscapeClosesWindowProperty =
DependencyProperty.RegisterAttached( "EscapeClosesWindow", typeof( bool ),
typeof( WindowKeysHandling ),
new FrameworkPropertyMetadata( false,
new PropertyChangedCallback( OnEscapeClosesWindowChanged ) ) );
/// <summary>
/// Handles changes to the 'EscapeClosesWindow' attached property.
/// </summary>
private static void OnEscapeClosesWindowChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// Prevent the following WPF Designer Warning :
// "Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance"
// can't be converted into "System.Windows.Window"
if (DesignerProperties.GetIsInDesignMode( new DependencyObject() ) == false)
{
Window targetWindow = sender as Window;
if (targetWindow != null)
{
if ((bool)e.NewValue == true)
{
targetWindow.PreviewKeyDown += new KeyEventHandler( Window_PreviewKeyDown );
}
else
{
targetWindow.PreviewKeyDown -= new KeyEventHandler( Window_PreviewKeyDown );
}
}
}
}
/// <summary>
/// Handle the 'PreviewKeyDown'-event on the Window
/// </summary>
private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
// If the escape key was pressed, close the window
if (e.Key == Key.Escape)
{
(sender as Window).Close();
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Experience:
More than 20 years with software design, architecture and development. More than 9 years with the .NET Framework.
Preferences:
C#, .NET 2.0, .NET 3.5, .NET 4.0, .NET 4.5, WPF (3, 4 and 4.5), MVVM, XAML, Silverlight, Windows Phone, Windows 8 Apps, ASP.NET, WCF, T-SQL and especially GUI-Development and GUI-Design.
I would say I am a dotNet Developer with a keen affinity for developing and enriching user interfaces.