将组合框的 Text 属性绑定到视图模型项,然后添加到那里的绑定集合中,例如,
Text="{Binding UserEnteredItem, UpdateSourceTrigger=LostFocus}"
将 UpdateSourceTrigger 更改为 LostFocus,因为默认 (PropertyChanged) 会将每个字符更改传达给您的视图模型。
// user entered value
private string mUserEnteredItem;
public string UserEnteredItem {
get {
return mUserEnteredItem;
}
set {
if (mUserEnteredItem != value) {
mUserEnteredItem = value;
TypePLCList.Add (mUserEnteredItem);
// maybe you want to set the selected item to user entered value
TypePLC = mUserEnteredItem;
}
}
}
// your selected item
private string mTypePLC;
public string TypePLC {
get {
return mTypePLC;
}
set {
if (mTypePLC != value) {
mTypePLC = value;
// notify change of TypePLC INPC
}
}
}
// your itemsource
public ObservableCollection <string> TypePLCList { set; private set;}