开发者

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

开发者 https://www.devze.com 2022-12-13 19:41 出处:网络
开发者_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 contro

开发者_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

精彩评论

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