locked
TextBox.KeyUp Handler Executes Before Data Binding Changes Propagate RRS feed

  • Question

  • Hello All,

    I have a TextBox whose TextProperty feeds a data binding chain. I want to take action and act on what the user typed into the TextBox when either 1) the user presses "Return" or 2) the TextBos loses focus.

    I'm currently attempting 1) by listening for KeyUp and, when it occurs, firing an event if the key that caused the event is "Return".

    The problem with the approach just described is that the data binding chain TextBox.TextProperty drives is not yet refreshed when my KeyUp handler executes, so the handlers for my event end up operating on old data.

    What suggestions does the community have to get around this problem?

    Thanks,
    David

     

    Monday, May 24, 2010 9:20 PM

Answers

  • The following seems to do the trick:

    private void OnKeyUp(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Return || e.Key == Key.Enter)
       m_alternateNameTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
    }
    
    private void OnSourceUpdated(object sender, DataTransferEventArgs e)
    {
      if (SelectedItemsCollection.Find(m_itemsComboBox.SelectedItem) >= 0)
       RaiseEvent(new RoutedEventArgs(ChangeInSelectedItemsEvent, this));
    }
    
    • Marked as answer by dtheese Tuesday, May 25, 2010 3:55 PM
    Tuesday, May 25, 2010 3:55 PM