我有一个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.Title
的ComboBox
,视图模型通知它,并创建一个新Job
项目并将其添加到JobList
。
你有什么想法吗?你能帮我吗?