João Lourenço João Lourenço - 1 month ago 26
C# Question

Windows Store App Textbox keeps losing focus

I have a simple application that has a

TextBox
and a
Canvas
. When the user touches the
Canvas
, I want to move the focus to the
TextBox
. So, on the
PointerReleased
event, I got:

private void canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}


I also subscribed to the
TextBox GotFocus
and
LostFocus
and noticed that, immediately after the
TextBox
got focused, it looses the focus. Why is it loosing focus and how can I solve it?

Thanks in advance.

Answer

This will work for me.

private void Canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}


private void textBoxMain_GotFocus(object sender, RoutedEventArgs e)
{
    textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}