Set focus on textbox in WPF

166,956

Solution 1

In XAML:

<StackPanel FocusManager.FocusedElement="{Binding ElementName=Box}">
   <TextBox Name="Box" />
</StackPanel>

Solution 2

Nobody explained so far why the code in the question doesn't work. My guess is that the code was placed in the constructor of the Window. But at this time it's too early to set the focus. It has to be done once the Window is ready for interaction. The best place for the code is the Loaded event:

public KonsoleWindow() {
  public TestWindow() {
    InitializeComponent();
    Loaded += TestWindow_Loaded;
  }

  private void TestWindow_Loaded(object sender, RoutedEventArgs e) {
    txtCompanyID.Focus();
  }
}

Solution 3

try FocusManager.SetFocusedElement

FocusManager.SetFocusedElement(parentElement, txtCompanyID)

Solution 4

txtCompanyID.Focusable = true;
Keyboard.Focus(txtCompanyID);

msdn:

There can be only one element on the whole desktop that has keyboard focus. In WPF, the element that has keyboard focus will have IsKeyboardFocused set to true.

You could break after the setting line and check the value of IsKeyboardFocused property. Also check if you really reach that line or maybe you set some other element to get focus after that.

Solution 5

Try this : MyTextBox.Focus ( );

Share:
166,956
priyanka.sarkar
Author by

priyanka.sarkar

Student

Updated on August 15, 2020

Comments

  • priyanka.sarkar
    priyanka.sarkar over 3 years

    How to set the focus on an TextBox element in WPF

    I have this code:

    txtCompanyID.Focusable = true;
    txtCompanyID.Focus();
    

    ...but it is not working.

    Any idea?

  • Brock Hensley
    Brock Hensley over 10 years
    What if the element you want to set focus to is the parent element o.O?
  • Todd Sprang
    Todd Sprang about 10 years
    I prefer this approach to the others above since it keeps in line with MVVM.
  • dbeachy1
    dbeachy1 about 9 years
    This is the most elegant answer and it doesn't require that you specify the parent as well. Thanks for this, it works great for me!
  • Philter
    Philter about 9 years
    FocusManager.SetFocusedElement(FocusManager.GetFocusScope(pa‌​rentElement), parentElement);
  • TravisWhidden
    TravisWhidden over 8 years
    This worked for me also. The rest was not. Thanks for the link, that was quite interesting. Also interesting that such a simple thing can be so complicated.
  • usefulBee
    usefulBee over 8 years
    The above answer works fine regardless if the container is a Gird or a StackPanel. Since the structure of your grid is not clear, it is hard to tell what could have went wrong. Nice to see alternatives though.
  • WPFKK
    WPFKK about 8 years
    Focused element is readonly right How can you set in xaml? I used this and it did not work <Style.Triggers> <Trigger Property="Validation.HasError" Value="True"> <Setter Property="FocusManager.FocusedElement" Value="{Binding Source={RelativeSource Self}}"/> </Trigger> </Style.Triggers>
  • usefulBee
    usefulBee about 8 years
    @user841612, check the following link and verify the Assembly and Namespace msdn.microsoft.com/en-us/library/…
  • OregonGhost
    OregonGhost over 7 years
    For me, this is also the only one to work correctly. Nice way.
  • shivani
    shivani almost 6 years
    logically true , after too much struggle this answer is pretty good and perfect as solution.
  • Joe Steele
    Joe Steele about 5 years
    This only works once. If you want to change the focus after the page has been built, you need to do it programmatically.
  • Simon Mourier
    Simon Mourier almost 5 years
    This answer deserves a better rank.
  • Adriaan Davel
    Adriaan Davel over 4 years
    Peter Huber's answer does this but explains that the window needs to be loaded first, which is why mine was not working
  • BoundForGlory
    BoundForGlory over 4 years
    this worked for me. the accepted answer didnt. Thanks
  • Dan
    Dan about 4 years
    not sure if version specific, but Focus() requires focus state parameter - e.g. txtCompanyId.Focus(FocusState.Keyboard)
  • MindRoasterMir
    MindRoasterMir over 3 years
    this works too. txtCompanyID.Focusable = true; Keyboard.Focus(txtCompanyID);
  • Rodri
    Rodri about 2 years
    Yes, in Loaded event works.