如何在 wpf 文本框中进行自动完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25683394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make autocomplete in wpf textbox?
提问by Ramji21
Here is the code I am using for search, it returns a value when we type a whole name, but I need a autocomplete textbox, that shows suggestions as I type a partial name.
这是我用于搜索的代码,它在我们键入全名时返回一个值,但我需要一个自动完成文本框,在我键入部分名称时显示建议。
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();
}
回答by Shaharyar
You can create an AutoCompleteTextBoxby using a simple textboxcontrol and a listboxcontrol. Here is a tutorialwhich will guide you through the whole process.
您可以AutoCompleteTextBox通过使用简单textbox控件和listbox控件来创建一个。这是一个教程,将指导您完成整个过程。
But if you don't want to create it yourself so you can just use an already created control by someone else. Download it from here.
但是,如果您不想自己创建它,那么您可以使用其他人已经创建的控件。从这里下载。
And here is the tutorialteaching you how to use that control.
这是教您如何使用该控件的教程。
回答by varsha
you can also add the AutoCompleteBoxinto the toolbox by clicking on it and then Choose Items, go to WPF Components, type in the filter AutoCompleteBox, which is on the System.Windows
您还可以AutoCompleteBox通过单击将其添加到工具箱中,然后选择项目,转到 WPF 组件,输入过滤器AutoCompleteBox,它位于System.Windows

