3

我有一个ComboBox它看起来像这样:

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

它与我的绑定ViewModel看起来像这样:

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 */ }
}

Job

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

真的,我想填写ComboBox工作清单。因此,如果用户指定Job在列表中,用户可以从列表中选择它,否则,他输入一个新Job.TitleComboBox,视图模型通知它,并创建一个新Job项目并将其添加到JobList

你有什么想法吗?你能帮我吗?

4

1 回答 1

4
  1. Create a string property in the viewModel something like 'SelectedJobName'
  2. Bind this property to Combobox.Text
  3. Wherever you want to use the entered value (Command, Presenter), check if selected value is not null and selectedJobName property value is not/matching.
于 2012-05-09T05:45:40.840 回答