Set focus on textbox in WPF
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 ( );
Comments
-
priyanka.sarkar over 3 years
How to set the focus on an
TextBox
element in WPFI have this code:
txtCompanyID.Focusable = true; txtCompanyID.Focus();
...but it is not working.
Any idea?
-
Brock Hensley over 10 yearsWhat if the element you want to set focus to is the parent element o.O?
-
Todd Sprang about 10 yearsI prefer this approach to the others above since it keeps in line with MVVM.
-
dbeachy1 about 9 yearsThis 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 about 9 yearsFocusManager.SetFocusedElement(FocusManager.GetFocusScope(parentElement), parentElement);
-
TravisWhidden over 8 yearsThis 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 over 8 yearsThe 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 about 8 yearsFocused 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 about 8 years@user841612, check the following link and verify the Assembly and Namespace msdn.microsoft.com/en-us/library/…
-
OregonGhost over 7 yearsFor me, this is also the only one to work correctly. Nice way.
-
shivani almost 6 yearslogically true , after too much struggle this answer is pretty good and perfect as solution.
-
Joe Steele about 5 yearsThis only works once. If you want to change the focus after the page has been built, you need to do it programmatically.
-
Simon Mourier almost 5 yearsThis answer deserves a better rank.
-
Adriaan Davel over 4 yearsPeter Huber's answer does this but explains that the window needs to be loaded first, which is why mine was not working
-
BoundForGlory over 4 yearsthis worked for me. the accepted answer didnt. Thanks
-
Dan about 4 yearsnot sure if version specific, but
Focus()
requires focus state parameter - e.g.txtCompanyId.Focus(FocusState.Keyboard)
-
MindRoasterMir over 3 yearsthis works too. txtCompanyID.Focusable = true; Keyboard.Focus(txtCompanyID);
-
Rodri about 2 yearsYes, in Loaded event works.