locked
How to set focus to a control?

    Question

  • Xaml:

            <StackPanel>
                <TextBox x:Name="textBox"></TextBox>
                <Button x:Name="button1" Click="button1_Click_1">Set focus</Button>
            </StackPanel>

    Code:

            private void button1_Click_1(object sender, RoutedEventArgs e)
            {
                textBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            }
    --Rob

    For unknown reasons, I totally am unable to use this method.

    The method always returns false no matter what controls I picked, which means the control is unfocus-able

    Does anyone know what causes this behaviour?

    Saturday, June 21, 2014 2:39 AM

All replies

  • That code as is will work. If it's not working for you you're doing something different.

    What control are you trying to focus and from where are you focusing it?

    Can you show some minimal code that demonstrates the problem?

    --Rob

    Saturday, June 21, 2014 3:15 AM
    Owner