Focus (the TextBox of) AutoCompleteBox in XAML

433 views Asked by At

I have an AutoCompleteBox in a C# WPF project. What I want is to have this box automatically focussed when the view is opened. I know there are some solutions out there in code, but I want to do this directly in the XAML (if possible).

So far I've tried the following, but it doesn't seem to do anything:

<controls:AutoCompleteBox x:Name="Items" ... Text="{Binding Search, Mode=TwoWay}">
    <controls:AutoCompleteBox.TextBoxStyle>
        <Style TargetType="TextBox">
            <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
        </Style>
    </controls:AutoCompleteBox.TextBoxStyle>
</controls:AutoCompleteBox>
1

There are 1 answers

5
Mohnkuchenzentrale On
<ContainerXYZ FocusManager.FocusedElement="{Binding ElementName=Items}">
    <controls:AutoCompleteBox x:Name="Items" ... Text="{Binding Search, Mode=TwoWay}">
</ContainerXYZ>

Something like this should do the trick