I am using a combo box in WPF App such that after 3 characters typed into the Combo Box, it queries SQL to generate a data table, which will then provide my combo box an ItemsSource, after converting that into a list. I am using LINQ to further filter that list as the user types more than 3 characters into my combo box. However, whenever the Item Source gets updated, the text in the combo box gets highlighted, and the user who is typing, will erase everything and begin back at 1 character, as expected when you highlight text and begin typing. Here is the error I am referring to:
(Sorry, values covered up as it is private info)
So, as you can see, my combo box has its' ItemsSource, but as it has been updated. However, the next letter I type in my combobox is going to be overwritten because what I have typed, "ANDE" is finding a close match and is being auto highlighted, the blue highlight over "11/2/1979." Is there a way to turn this suggestion portion off?
Here is my XAML, in the event that is useful, as well as my update the itemSource code:
XAML
<ComboBox x:Name="patientComboBox"
Text="" HorizontalAlignment="Left"
Margin="21,36,0,0" VerticalAlignment="Top"
Width="269" IsEditable="True
KeyDown="patientComboBox_KeyDown"
DropDownClosed="patientComboBox_DropDownClosed"/>
ComboBox_KeyDown
private async void patientComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (patientComboBox.Text.Length < 3)
{
PublicVars.data = new System.Data.DataTable();
}
else
{
PublicVars.patientSearchText = patientComboBox.Text;
PublicVars.searchComplete = false;
PublicVars.currentNameSearch = patientComboBox.Text;
//Dont write over the thread if it is currently running
if (!worker.IsBusy)
await Task.Run(() => worker.RunWorkerAsync());
//wait for the async to run to continue
check:
if (PublicVars.currentNames != null && PublicVars.searchComplete == true)
patientComboBox.ItemsSource = PublicVars.currentNames;
else
{
goto check;
}
patientComboBox.IsDropDownOpen = true;
}
}
Background_Worker_DoWork
private async void Worker_DoWork(object sender, DoWorkEventArgs e)
{
if (PublicVars.data.Rows.Count != 0)
{
PublicVars.currentNames = new List<string>();
await Task.Run(() => PublicVars.filterList = PublicVars.data.AsEnumerable().Where(x => x["LastName"].ToString() == PublicVars.patientSearchText).ToList());
foreach(DataRow row in PublicVars.filterList)
{
PublicVars.currentNames.Add(row["LastName"].ToString() + ", " + row["FirstName"].ToString() + " -- " + row["DOB"].ToString());
}
PublicVars.searchComplete = true;
}
else
{
PublicVars.topPatients = new List<NameSearchObj>();
PublicVars.currentNames = new List<string>();
SqlConnection cnn = new SqlConnection("...");
string sqlCommand = "...";
cnn.Open();
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dtable = new System.Data.DataTable();
SqlDataAdapter dscmd = new SqlDataAdapter(sqlCommand, cnn);
dscmd.Fill(dtable);
foreach (System.Data.DataRow row in dtable.Rows)
{
PublicVars.topPatients.Add(new NameSearchObj(row["LastName"].ToString() + ", " + row["FirstName"].ToString() + " -- " + row["DOB"].ToString(), row["PtKey"].ToString()));
}
foreach (NameSearchObj searcher in PublicVars.topPatients)
{
string[] convertName = searcher._nameDOBCombo.Split(' ');
string finalName = convertName[0] + " " + convertName[1] + " " + convertName[2] + " " + convertName[3];
PublicVars.currentNames.Add(finalName);
}
PublicVars.data = dtable;
PublicVars.searchComplete = true;
}
}