Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TextBoxCloseWindowBehavior.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Input;
6 
7 namespace SiliconStudio.Presentation.Behaviors
8 {
9  /// <summary>
10  /// A behavior that can be attached to a <see cref="TextBox"/> and will close the window it is contained in when <see cref="Key.Enter"/> is pressed.
11  /// A command can ben executed before closing the window, you can use the <see cref="CloseWindowBehavior{T}.Command"/> and <see cref="CloseWindowBehavior{T}.CommandParameter"/> property of this behavior.
12  /// </summary>
13  public class TextBoxCloseWindowBehavior : CloseWindowBehavior<TextBox>
14  {
15  /// <summary>
16  /// Identifies the <see cref="IsEnabled"/> dependency property.
17  /// </summary>
18  public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(TextBoxCloseWindowBehavior));
19 
20  /// <summary>
21  /// Gets or sets whether this behavior is currently enabled.
22  /// </summary>
23  public bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { SetValue(IsEnabledProperty, value); } }
24 
25  /// <inheritdoc/>
26  protected override void OnAttached()
27  {
28  base.OnAttached();
29  AssociatedObject.KeyUp += KeyUp;
30  }
31 
32  /// <inheritdoc/>
33  protected override void OnDetaching()
34  {
35  AssociatedObject.KeyUp -= KeyUp;
36  base.OnDetaching();
37  }
38 
39  /// <summary>
40  /// Raised when the associated button is clicked. Close the containing window
41  /// </summary>
42  private void KeyUp(object sender, KeyEventArgs e)
43  {
44  if (e.Key != Key.Enter || !IsEnabled)
45  return;
46 
47  Close();
48  }
49  }
50 }
A behavior that can be attached to a TextBox and will close the window it is contained in when Key...