In my windows phone 7 App I have a single line textbox. When the user presses {ENTER} I want to accept the textbox value and switch the textbox back into normal non-edit mode.
Basically, is there a way to programmatically cancel editing a textbox?
I have tried force the Visual State Manager into Normal mode which does change the visual style, but the textbox is still in edit mode and on-screen keyboard is still showing.
VisualStateManager.GoToState(
this.MyTextBox,
"Normal",
true);
VisualStateManager.GoToState(
this.MyTextBox,
"Unfocused",
true);
Also tried to programmatically select a parent control but that doesn't seem to work either.
I think I must be missing something simple, someone must have done this a million times - any help much appreciated.
Thanks,
Update: I tried to set focus to another Control but that wasn't working, the SIP keyboard would never disappear.
But I figured it out using another method. The trick was to use the IsReadOnly flag. When lost focus or enter was pressed I set the control back to read only which updates the style. All I had to do was update my visual stlyes so it looked right and now works perfectly.
For what it's worth, my code now looks something like this:
private void MyTextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.MyTextBox.IsReadOnly = false;
this.MyTextBox.SelectAll();
}
private void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
this.MyTextBox.IsReadOnly = true;
}
private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.MyTextBox.IsReadOnly = true;
VisualStateManager.GoToState(
this.MyTextBox,
"ReadOnly",
true);
VisualStateManager.GoToState(
this.MyTextBox,
"Unfocused",
true);
VisualStateManager.GoToState(
this.MyTextBox,
"Valid",
true);
}
}