在 c# wpf 窗口中将文本框转换为自动完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23199287/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:35:17  来源:igfitidea点击:

Convert a textbox to autocomplete in c# wpf window?

c#wpfautocompletetextbox

提问by samson

I am building a desktop application using WPF in VisualStudio 2013.

我正在 VisualStudio 2013 中使用 WPF 构建桌面应用程序。

I have a WPF window with a textbox.

我有一个带有文本框的 WPF 窗口。

How can I convert this textbox to autocomplete?

如何将此文本框转换为自动完成?

回答by Bastian M.K. Ohta

The easiest way is to use a ComboBox rather than a TextBox. The ComboBox supports autocomplete. In WPF you can then use a TextBox template to override the standard ComboBox template. My advice is to use the ComboBox as it is.

最简单的方法是使用 ComboBox 而不是 TextBox。ComboBox 支持自动完成。在 WPF 中,您可以使用 TextBox 模板来覆盖标准 ComboBox 模板。我的建议是按原样使用 ComboBox。

    <ComboBox Name="CBox" Margin="0" VerticalAlignment="Top" IsEditable="True" >
        <ComboBoxItem>One</ComboBoxItem>
        <ComboBoxItem>Two</ComboBoxItem>
        <ComboBoxItem>Three</ComboBoxItem>
        <ComboBoxItem>Four</ComboBoxItem>
        <ComboBoxItem>Two2</ComboBoxItem>
        <ComboBoxItem>Two2b</ComboBoxItem>
    </ComboBox>

You can also use the AutoComplete TextBox from CodePlex: http://wpfactb.codeplex.com/

您还可以使用 CodePlex 中的 AutoComplete TextBox:http: //wpfactb.codeplex.com/