Advertisement
nathanaw

ForwardFocusBehavior

Oct 27th, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1.     public class ForwardFocusBehavior
  2.     : System.Windows.Interactivity.Behavior<FrameworkElement>
  3.     {
  4.  
  5.         private FrameworkElement ForwardTo = null;
  6.  
  7.         protected override void OnAttached()
  8.         {
  9.             base.OnAttached();
  10.             this.ForwardTo = (FrameworkElement)Find(
  11.                 AssociatedObject,
  12.                 d => d.GetType() == ForwardToChildOfType);
  13.             this.ForwardFrom.GotKeyboardFocus += AssociatedObject_GotKeyboardFocus;
  14.         }
  15.  
  16.         protected override void OnDetaching()
  17.         {
  18.             base.OnDetaching();
  19.             this.ForwardFrom.GotKeyboardFocus -= AssociatedObject_GotKeyboardFocus;
  20.             this.ForwardTo = null;
  21.         }
  22.  
  23.         void AssociatedObject_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
  24.         {
  25.             if (null == ForwardTo)
  26.             {
  27.                 this.ForwardTo = (FrameworkElement)Find(
  28.                     AssociatedObject,
  29.                     d => d.GetType() == ForwardToChildOfType);
  30.             }
  31.  
  32.             // Detect a reverse traversal request.
  33.             if (e.OldFocus == ForwardTo
  34.         && e.NewFocus == ForwardFrom
  35.         && e.KeyboardDevice.IsKeyDown(Key.Tab))
  36.             {
  37.                 this.ForwardFrom.MoveFocus(
  38.             new TraversalRequest(FocusNavigationDirection.Previous));
  39.             }
  40.             else if (this.ForwardFrom.IsKeyboardFocused)
  41.             {
  42.                 this.ForwardTo.Focus();
  43.             }
  44.         }
  45.  
  46.  
  47.         public FrameworkElement ForwardFrom
  48.         {
  49.             get { return (FrameworkElement)GetValue(ForwardFromProperty); }
  50.             set { SetValue(ForwardFromProperty, value); }
  51.         }
  52.  
  53.         public static readonly DependencyProperty ForwardFromProperty =
  54.             DependencyProperty.Register("ForwardFrom", typeof(FrameworkElement),
  55.             typeof(ForwardFocusBehavior), new UIPropertyMetadata(null));
  56.  
  57.         public Type ForwardToChildOfType
  58.         {
  59.             get { return (Type)GetValue(ForwardToChildOfTypeProperty); }
  60.             set { SetValue(ForwardToChildOfTypeProperty, value); }
  61.         }
  62.  
  63.         public static readonly DependencyProperty ForwardToChildOfTypeProperty =
  64.             DependencyProperty.Register("ForwardToChildOfType", typeof(Type),
  65.             typeof(ForwardFocusBehavior), new UIPropertyMetadata(null));
  66.  
  67.  
  68.  
  69.  
  70.  
  71.         /// <summary>
  72.         /// Searches for a child element of a DependencyObject that
  73.     /// matches the lambda condition given.
  74.         /// </summary>
  75.         /// <param name="obj"></param>
  76.         /// <param name="name"></param>
  77.         private static DependencyObject Find(DependencyObject obj, Func<DependencyObject, bool> condition)
  78.         {
  79.             if ((obj == null))
  80.                 return null;
  81.  
  82.             if (condition(obj))
  83.                 return obj;
  84.  
  85.             DependencyObject result = null;
  86.             for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  87.             {
  88.                 result = Find(VisualTreeHelper.GetChild(obj, i), condition);
  89.  
  90.                 if (null != result)
  91.                     return result;
  92.             }
  93.  
  94.             return null;
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement