ComboBox文字突出显示&每当ItemsSource更改时都会删除

时间:2016-10-25 14:03:47

标签: c# wpf xaml combobox

我在WPF应用程序中使用一个组合框,以便在组合框中键入3个字符后,它会查询SQL以生成数据表,然后在将其转换为列表后,将为我的组合框提供ItemsSource。我正在使用LINQ进一步过滤该列表,因为用户在我的组合框中键入了超过3个字符。但是,每当项目源更新时,组合框中的文本都会突出显示,正在键入的用户将删除所有内容并以1个字符开头,正如您突出显示文本并开始键入时所预期的那样。这是我指的错误:

Example of my Combo Box

  

(对不起,价值被掩盖,因为它是私人信息)

所以,正如你所看到的,我的组合框有它的' ItemsSource,但已更新。但是,我在组合框中键入的下一个字母将被覆盖,因为我输入的是" ANDE"正在寻找一个紧密的匹配,并正在自动突出显示,蓝色突出显示" 11/2/1979。"有没有办法关掉这个建议部分?

这是我的XAML,在有用的情况下,以及我更新itemSource代码:

  

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 个答案:

答案 0 :(得分:0)

我还没有尝试过你的代码,但我认为你可以设置TextSearch.TextPath =&#34; dummy&#34;或者不是下拉列表中对象属性名称的任何其他值。

答案 1 :(得分:0)

我通过使用文本框解决了我的问题,好像它是一个&#34;搜索框&#34;用户可以输入搜索条件,并调整它旁边的Combobox的itemsSource。对解决方案不满意,但它现在完成了任务。