Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard.Focus does not work on text box in WPF

Tags:

I am banging my head on what looks like such a simple problem to fix in wpf but i have yet to discover why i can't get my app to behave according to my plan.

I have a small search box that pops-up in my wpf application when user presses ctrl+f. All i want is for the caret to be flashing inside the search box text box, ready to take whatever user input without the user having to click on it. Here is the xaml code for the text box which is visible, enabled, hit testable, tabstopable and focusable.

   <TextBox x:Name="SearchCriteriaTextBox" Text="{Binding SearchCriteria}" Focusable="True" IsEnabled="True" IsTabStop="True" IsHitTestVisible="True" Style="{DynamicResource SearchTextBoxStyle}" Grid.Column="1" Margin="5,10,0,5" /> 

In the code behind, i have this method called when the visibility of the search box is affected. the search box is loaded at the start of the app.

    /// <summary>     /// Handles events triggered from focusing on this view.     /// </summary>     /// <param name="sender">The sender.</param>     /// <param name="dependencyPropertyChangedEventArgs">The key event args.</param>     private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)     {         if (!((bool) dependencyPropertyChangedEventArgs.NewValue))         {             return;         }          SearchCriteriaTextBox.Focus();         Keyboard.Focus(SearchCriteriaTextBox);         SearchCriteriaTextBox.Select(0, 0);          if (SearchCriteriaTextBox.Text.Length > 0)         {             SearchCriteriaTextBox.SelectAll();         }     } 

The problem is, code gets called, component becomes IsFocused=true but does not gain keyboard focus. Am I missing something? Unless another control keeps hold ferociously to the keyboard focus which i am pretty sure i didn't code, why would this piece of rather simple code would not work properly.

like image 985
legrandviking Avatar asked Dec 19 '12 15:12

legrandviking


People also ask

What does .focus do in C#?

The Focus method returns true if the control successfully received input focus. The control can have the input focus while not displaying any visual cues of having the focus. This behavior is primarily observed by the nonselectable controls listed below, or any controls derived from them.

What is TextBox in WPF?

WPF TextBox control represent a control that can be used to display or edit unformatted text. The TextBox class in C# represents the control. The XAML <TextBox> element represents the TextBox control in UI. The code samples in this article show how to use XAML TextBox element to create a TextBox control in WPF and C#.


1 Answers

As a workaround, you could try using the Dispatcher to set the focus at a later DispatcherPriority, such as Input

Dispatcher.BeginInvoke(DispatcherPriority.Input,     new Action(delegate() {          SearchCriteriaTextBox.Focus();         // Set Logical Focus         Keyboard.Focus(SearchCriteriaTextBox); // Set Keyboard Focus      })); 

From the description of your problem, it sounds like you don't have Keyboard focus set. WPF can have multiple Focus Scopes, so multiple elements can have Logical Focus (IsFocused = true), however only one element can have Keyboard Focus and will receive keyboard input.

The code you posted should set the focus correctly, so something must be occurring afterwards to move Keyboard Focus out of your TextBox. By setting focus at a later dispatcher priority, you'll be ensuring that setting keyboard focus to your SearchCriteriaTextBox gets done last.

like image 188
Rachel Avatar answered Oct 13 '22 08:10

Rachel