gpt4 book ai didi

WPF - 组合框 - 当用户在组合中输入文本时添加项目

转载 作者:行者123 更新时间:2023-12-04 21:56:31 24 4
gpt4 key购买 nike

我有一个 ComboBoxObservableCollection 绑定(bind).当用户在 ComboBox 中输入文本时我该怎么办? ,如果项目不在列表中,代码会自动将新项目添加到列表中吗?

<ComboBox Name="cbTypePLC"
Height="22"
ItemsSource="{StaticResource TypePLCList}"
SelectedItem="{Binding TypePLC}" IsReadOnly="False" IsEditable="True">
</ComboBox>

最佳答案

将组合框的 Text 属性绑定(bind)到 View 模型项,然后添加到那里的绑定(bind)集合中,例如,

Text="{Binding UserEnteredItem, UpdateSourceTrigger=LostFocus}"

将 UpdateSourceTrigger 更改为 LostFocus,因为默认 (PropertyChanged) 会将每个字符更改传达给您的 View 模型。
// 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;}

关于WPF - 组合框 - 当用户在组合中输入文本时添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17805798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com