Question

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?

Was it helpful?

Solution

Why don't you try DataContextChangedEvent. Bringing focus is always a pain in WPF... we have to relay on code behind file for it...

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top