AutoCompleteBox 在 C# wpf 中无法获得正确的值
我目前正在开发一个 WPF C# 项目。我正在使用 AutoCompleteBox WPF 控件,但在获取开箱即用的值时遇到问题。
假设自动完成框用于服务器名称,当我输入“loc”时,弹出框将显示“localhost”,然后我从下拉框中选择值。
然后,当我尝试提交表单并尝试获取该框的值时,它将获取我键入的值而不是我选择的值,即该值将为“loc”。
下面是我用来填充控件的自动完成项的代码,
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
List<string> serverArr = new List<string>();
while (reader.Read())
{
serverArr.Add(reader["his_server"].ToString());
}
txtServer.ItemsSource = serverArr;
}
我通过说 txtServer.Text; 从自动完成框中获取值;
更新
按照@Tom Studee的建议,我尝试使用txtServer.selectedItem,当选择自动完成中的项目时,它可以正常工作。但是,如果键入的值不在下拉自动完成范围内,则会失败并出现空指针异常。
I am currently working on a WPF C# project. I am using the AutoCompleteBox WPF control but I am having a problem getting the value out of the box.
Assuming that the autocomplete box is for a server name, when I type 'loc' the popup box will show up 'localhost' and I select the value from the dropdown box.
When I then try and submit the form and attempt to get the value of the box it will get the value of what I type not what I selected i.e. the value will be 'loc'.
Below is the code I am using to populate the AutoComplete items for the control
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
List<string> serverArr = new List<string>();
while (reader.Read())
{
serverArr.Add(reader["his_server"].ToString());
}
txtServer.ItemsSource = serverArr;
}
I am getting the value from the autocompletebox by saying txtServer.Text;
Update
As suggested by @Tom Studee I tried using the txtServer.selectedItem which works fine when an item from the auto complete is selected. However, if a value is typed which isn't inside the drop down auto complete then it fails with a Null Pointer Exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
.SelectedItem
属性代替.Text
。Instead of
.Text
use the.SelectedItem
property.你也许能够
You might be able to