0

Is it possible to add somehow autocomplete suggestions to textbox in a WPF application? Like where I bind the suggestions to a DataTable or List of Strings? Is this possible with a textbox?

 <TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     VerticalContentAlignment="Center" >
        <TextBox.InputBindings>
          <KeyBinding Command="{Binding EnterKeyPressedCommand}" Key="Return" />
        </TextBox.InputBindings>
      </TextBox>
  • 1
    `Is it possible to add somehow autocomplete suggestions to textbox in a WPF application` *yes*. Can you please show us what you have tried, the code above doesn't warrant what you are asking about. – Trevor Jan 31 '20 at 14:08
  • i tried nothing yet just did some research but i didn't found anything about that for a simple textbox – DataLordDev Jan 31 '20 at 14:31
  • `i tried nothing yet`, please do before posting here. We help with specific issue's users have *tried* already. Right now the question is seeking more of a *"code it for me"* it seems. – Trevor Jan 31 '20 at 14:35
  • I tried like things with external libaries with autocompletion boxes but don't want to buy license for this – DataLordDev Jan 31 '20 at 14:45
  • You really found [nothing free](https://www.c-sharpcorner.com/article/wpf-auto-completesuggestion-text-box-control2/)? [Nothing](https://stackoverflow.com/questions/51684857/wpf-suggestion-textbox)? [Nothing at all?](https://github.com/Nimgoble/WPFTextBoxAutoComplete/) – Kilazur Jan 31 '20 at 15:05
  • Nothing useful have worked only for forms I found something but I use wpf – DataLordDev Jan 31 '20 at 15:14

1 Answers1

0

If you are having trouble trying to find out where to start, there are actually several steps involved and likely SEVERAL ways to do this.

Off the top of my head, you could create a hidden ListBox that appears under your TextBox that contains your suggestions (make sure the ListBox sizes to content). As text changes, you can use a simple TestChanged event.

XAML:

<TextBox x:Name="someTextbox" 
    TextChanged="someTextbox_TextChanged"
</TextBox>

Code Behind:

    private void someTextbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        // Call method to check for possible suggestions.
        // Display Listbox with suggested items.
    }

Then, clicking on an item in the Listbox would update the text.
NOTE: You will need some way to prevent the event from running logic when a user selects a suggestion form the ListBox

Now for MVVM:

private string _SomeTextbox = "";
public string SomeTextbox
{
    get { return _SomeTextbox; }
    set
    {
        _SomeTextbox = value;
        OnPropertyChanged(new PropertyChangedEventArgs("SomeTextbox"));

        // Call method to check for possible suggestions.
        // Display Listbox with suggested items.
    }
}

With MVVM, you can bind the ListBox visibility and content with relative ease and then display it as needed.

ANOTHER way to do this is to edit the TextBox default to have a built in ListBox. This path is WAY more complicated though.

Hope this gets you started.

David Bentley
  • 824
  • 1
  • 8
  • 27