krische
krische

Reputation: 1010

"Submit" event for wpf editable combobox?

So I have this editble combobox in my wpf c# application. I am able to use the 'SelectionChanged' event correctly for when the user uses the drop down list.

However, I am not able to figure out how to get an event when the user "submits" their typed text in the edit box. I've tried 'TextInput' event, but that never seems to get triggered (I just call a function with a simple Debug.WriteLine("test");)

I have tried PreviewTextInput, but that gets triggered for each character. I'm looking for something like the user types what they want and hits the Enter key or clicks off the control.

Any ideas?

Upvotes: 1

Views: 657

Answers (1)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104821

Bind the Text property to the underlying DataContext.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">
  <Window.DataContext>                            
    <local:Contact/>
  </Window.DataContext>
  <StackPanel>
    <ComboBox Text="{Binding MyValue}" IsEditable="True"/>
    <TextBlock Text="{Binding MyValue}"/>
  </StackPanel>
</Window>

The underlying object should implement INotifyPropertyChanged:

public class Contact : INotifyPropertyChanged
{
  private string _MyValue;
  public string MyValue
  {
    get { return _MyValue; }
    set
    {
      _MyValue = value;
      if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("MyValue"));
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;
}

Upvotes: 1

Related Questions