Binding editable combobox and detecting inserted text in wpf

I have ComboBoxwhat it looks like:

<ComboBox
    ItemsSource="{Binding JobList}"
    SelectedValue="{Binding Job,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
    DisplayMemberPath="Title"
    SelectedValuePath="Id"
    IsEditable="True"
    StaysOpenOnEdit="True"
    />

and its binding to mine ViewModel, which is as follows:

public class ViewModel {
    // this will fill from a database record for a person
    public Job Job {
        get { return _job; }
        set {
            if(value == _job) return;
            _job = value;
            OnPropertyChanged( () => Job );
        }
    }
    // this will fill from all jobs records in database
    public ObservableCollection<Job> JobList 
    { /* do same as Job to implementing INotifyPropertyChanged */ }
}

and Job:

public class Job {
    public int Id { get; set; }
    public string Title { get; set; }
}

Indeed, I want to populate the list with ComboBoxassignment. Therefore, if the user specified Jobin the list, the user can select it from the list, otherwise he enters a new one Job.Titlein ComboBox, the view model notifies about it and creates a new element Job, and also add it to JobList.

Do you have any ideas? Could you help me?

+3
source share
1 answer
  • string Model, , "SelectedJobName"
  • Combobox.Text
  • (Command, Presenter), , null JobName.
+4

All Articles