开发者

AutoCompleteBox does not get correct value in C# wpf

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 ty开发者_如何学JAVAped which isn't inside the drop down auto complete then it fails with a Null Pointer Exception.


Instead of .Text use the .SelectedItem property.


You might be able to

   string cbValue;
   if (SelectedIndex == -1) cbValue = .Text; else cbValue = .SelectedItem;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜