Is there a way to programmatically cancel editing a text field?

In my Windows Phone 7 application, I have a single line text box. When the user presses {ENTER}, I want to accept the value of the text field and switch the text field back to normal without editing.

Basically, is there a way to programmatically cancel editing a text field?

I tried to get Visual State Manager to normal mode, which changes the visual style, but the text field is still in edit mode and the on-screen keyboard is still displayed.

            VisualStateManager.GoToState(
                this.MyTextBox,
                "Normal",
                true);

            VisualStateManager.GoToState(
                this.MyTextBox,
                "Unfocused",
                true);

Also tried to programmatically select the parent control, but this also does not work.

I think I should skip something simple, someone must have done it a million times - any help that is greatly appreciated.

Thank,

. , , SIP .

. , IsReadOnly. , , . , , stlyes, .

, :

    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);
       }
   }
+5
2

:

  • Call Focus() - (, ?)
  • , TextBox ( , ).
+3

, :

private void SearchBox_KeyUp(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Enter && !String.IsNullOrEmpty(SearchBox.Text))
  {
    (sender as TextBox).IsEnabled = false;
    (sender as TextBox).IsEnabled = true;

    // Process search term here...
  }
}
0

All Articles