Reputation: 16121
I am trying to implement SelectAll (text) functionality for all TextBoxes in my WPF application. I found how to do that here. However, the first TextBox on my form is not automatically focused. I try to remedy that in the Window_Loaded eventhandler by simply using firstTextBox.Focus
. That works, but the Text property of this TextBox is set through binding and it seems that this happens after the Window_Loaded event. So, I end up with a first textbox that is initially focused, but has not its Text selected. It seems I need to hook up to a different Event. Which one?
Upvotes: 0
Views: 472
Reputation: 3496
Why don't you try DataContextChangedEvent. Bringing focus is always a pain in WPF... we have to relay on code behind file for it...
Upvotes: 1
Reputation: 46595
Try using FocusManager in the XAML of your window:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
FocusManager.FocusedElement="{Binding ElementName=firstTextBox}">
<Grid>
<TextBox Name="firstTextBox" />
</Grid>
</Window>
Upvotes: 1