public static void Focus( UIElement element ) { if( !element.Focus() ) { element.Dispatcher.BeginInvoke( DispatcherPriority.Input, new ThreadStart( delegate() { element.Focus(); } )); } }
/// <summary> /// On current visual changed, change focus as desired /// </summary> private void FocusCurrentVisual() { System.Windows.UIElement element = CurrentVisual as System.Windows.UIElement; if (element == null) { return; } bool focusElement; if (CurrentNavigator is SearchNavigator && CurrentVisual is SearchViewControl) { // Search view control implements custom focus, don't override it focusElement = false; } else { // For anything else, focus current visual focusElement = true; } if (focusElement) { if (!element.Focus()) { element.MoveFocus( new System.Windows.Input.TraversalRequest( System.Windows.Input.FocusNavigationDirection.First ) ); } } }
/// <summary> /// Gives the focus to the given UIElement. /// </summary> /// <param name="element">The UIElement which has to get the focus.</param> /// <remarks>Giving the focus will be done using the target element dispatcher with the <see cref="System.Windows.Threading.DispatcherPriority.Render" /> priority.</remarks> public static void GiveFocus(UIElement element) { element.Dispatcher.BeginInvoke(new Action(delegate { element.Focus(); Keyboard.Focus(element); }), DispatcherPriority.Render); }
private IObservable<Vector2> GetObservable(UIElement target, string eventName, UIElement relative, Func<Point, Point> translate) { return Observable.FromEventPattern<MouseEventArgs>(target, eventName).Select( e => { target.Focus(); return ToVector2(translate(e.EventArgs.GetPosition(relative))); }); }
/// <summary> /// Gives the focus to the given UIElement with a Callback. /// </summary> /// <param name="element">The UIElement which has to get the focus.</param> /// <param name="actionOnFocus">The callback which will be called when the control got the focus. It will called just before the element.Focus will called and the KeyboardFocus will be set.</param> /// <remarks>Giving the focus will be done using the target element dispatcher with the <see cref="System.Windows.Threading.DispatcherPriority.Render" /> priority.</remarks> public static void GiveFocus(UIElement element, Action actionOnFocus) { element.Dispatcher.BeginInvoke(new Action(() => { actionOnFocus(); element.Focus(); Keyboard.Focus(element); }), DispatcherPriority.Render); }
private void PutFocus(UIElement uiElement) { if (ShouldUseKeyboardFocusMethod.IsChecked.GetValueOrDefault()) { Keyboard.Focus(uiElement); } else { uiElement.Focus(); } }
private void SetFocus(UIElement element) { if (element.Focusable) { element.Focus(); } else { element.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); } }