0
votes

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:

Example of my Combo Box

(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;
        }            
    }
2

2 Answers

0
votes

I haven't tried it with your code, but I think you can set TextSearch.TextPath="dummy" or to any other value that is not a name of the properties of the objects in the dropdown list.

0
votes

I solved my issue by just using a Textbox as if it were a "search box" where a user may enter search criteria, and that will adjust the itemsSource of the Combobox it is positioned next to. Not happy with the solution, but it accomplishes the task for now.