Skip to main content
 首页 » 编程设计

c#之如何在 wpf 文本框中自动完成

2024年02月06日11mfryf

这是我用于搜索的代码,当我们输入全名时它会返回一个值,但我需要一个自动完成文本框,它会在我输入部分名称时显示建议。

private void textBox3_KeyUp_1(object sender, System.Windows.Input.KeyEventArgs e)//Name Search 
{ 
    SqlConnection con = new SqlConnection(constr); 
    con.Open(); 
    SqlCommand cmd = con.CreateCommand(); 
    cmd.CommandType = CommandType.Text; 
    cmd.CommandText = "Select [Patient ID],[Patient Name],[Gender],[StudyDateTim],[Modality],[Study Name] From RepView Where [Patient Name] like '%" + textBox3.Text + "%'"; 
    cmd.ExecuteNonQuery(); 
    DataTable dt = new DataTable(); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    da.Fill(dt); 
    dataGrid1.ItemsSource = dt.DefaultView; 
    con.Close(); 
} 

请您参考如下方法:

您可以使用简单的文本框 控件和列表框 控件来创建AutoCompleteTextBox。这是一个tutorial它将指导您完成整个过程。

但是如果您不想自己创建它,那么您可以使用其他人已经创建的控件。从here下载.

这里是 tutorial教您如何使用该控件。