Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
FocusManager.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 
5 namespace SiliconStudio.Presentation.Core
6 {
7  /// <summary>
8  /// This class hold the <see cref="IsFocusedProperty"/> attached dependency property that allows to give the focus to a control using bindings.
9  /// </summary>
10  public static class FocusManager
11  {
12  /// <summary>
13  /// Identify the IsFocused attached dependency property.
14  /// </summary>
15  public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached("IsFocused", typeof(bool), typeof(FocusManager), new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
16 
17  /// <summary>
18  /// Gets whether the given object has currently the focus.
19  /// </summary>
20  /// <param name="obj">The object. If it is not an <see cref="UIElement"/>, this method will return <c>false</c>.</param>
21  /// <returns><c>true</c> if the given object has the focus, false otherwise.</returns>
22  public static bool GetIsFocused(DependencyObject obj)
23  {
24  var uiElement = obj as UIElement;
25  return uiElement != null && uiElement.IsFocused;
26  }
27 
28  /// <summary>
29  /// Gives the focus to the given object.
30  /// </summary>
31  /// <param name="obj">The object that should get the focus.</param>
32  /// <param name="value">The state of the focus. If value is <c>true</c>, the object will get the focus. Otherwise, this method does nothing.</param>
33  public static void SetIsFocused(DependencyObject obj, bool value)
34  {
35  obj.SetValue(IsFocusedProperty, value);
36  }
37 
38  /// <summary>
39  /// Raised when the <see cref="IsFocusedProperty"/> dependency property is modified.
40  /// </summary>
41  /// <param name="obj">The dependency object.</param>
42  /// <param name="e">The event arguments.</param>
43  private static void OnIsFocusedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
44  {
45  var uiElement = (UIElement)obj;
46  if ((bool)e.NewValue)
47  {
48  uiElement.Focus();
49  }
50  }
51  }
52 }
static bool GetIsFocused(DependencyObject obj)
Gets whether the given object has currently the focus.
Definition: FocusManager.cs:22
static void SetIsFocused(DependencyObject obj, bool value)
Gives the focus to the given object.
Definition: FocusManager.cs:33
This class hold the IsFocusedProperty attached dependency property that allows to give the focus to a...
Definition: FocusManager.cs:10