开发者

WPF - auto suggest text as a person types into a text box control

开发者_Python百科What is the best way to implement an auto-suggest feature for a textbox in WPF? I have found various article that are convoluted (and old) and some also suggest that there is a control available for this (but its not in my current WPF toolkit). What's the latest/best method for implementing auto-suggest as a user is typing in to a textbox?


First approach is to use ComboBox because it already have such functionality. You can use TextSearch feature of it. To enable this feature use this code (sorry, it is quick and dirty):

<ComboBox ItemsSource="{Binding AutoSuggestionVariants}">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="TextSearch.Text" Value="{Binding}" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

Also if you need it, you can restyle combo box such that it will look like a text box (remove button and popup list).

Another approach is to use CollectionView. This article describes how to do about the same feature as TextSearch for combo box. I think you can adopt this idea to text box.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜