我目前正在开发一个 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 从自动完成框中获取值;
更新
As suggested by @Tom Studee I tried using the txtServer.selectedItem which works fine when an item from the auto complete is selected. 但是,如果键入的值不在下拉自动完成中,则它会失败并出现空指针异常。