3

I build a little WPF TextBox that checks it's content if it is valid. Now i want to implement the possibility to give suggestions. But not like the samples in the internet where a list with suggestions pops up. I am looking for a sample that does it with the selection of the TextBox like this:


If there is a specific name that i can look up or any sample code that you know, please let me know.

  • Hmm, It's a little bit unclear. You want a dictionnary ? Or that list of suggestions filled by yourself via a database or something ? – Cesar Aug 04 '18 at 11:04
  • I did similar but with popup showing the suggested list with textbox auto-filling – RackM Aug 04 '18 at 11:32
  • Yes popups work fine, but they just didn't fit my need in that situation, since there is only one suggestion at the time. In addition to that there i needed suggestions for every "word" which would look strange but thanks for your efford guys. – Alexander Grasberger Aug 04 '18 at 19:06

1 Answers1

8

After a lot of fighting with WPF, I have a proof of concept working for you:

MainWindow.xaml

<Window x:Class="Solutions.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="SuggestionBox" Width="200"
                 />
    </Grid>
</Window>

MainWindow.xaml.cs:

using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Solutions
{
    public partial class MainWindow : Window
    {
        private static readonly string[] SuggestionValues = {
            "England",
            "USA",
            "France",
            "Estonia"
        };

        public MainWindow()
        {
            InitializeComponent();
            SuggestionBox.TextChanged += SuggestionBoxOnTextChanged;
        }

        private string _currentInput = "";
        private string _currentSuggestion = "";
        private string _currentText = "";

        private int _selectionStart;
        private int _selectionLength;
        private void SuggestionBoxOnTextChanged(object sender, TextChangedEventArgs e)
        {
            var input = SuggestionBox.Text;
            if (input.Length > _currentInput.Length && input != _currentSuggestion)
            {
                _currentSuggestion = SuggestionValues.FirstOrDefault(x => x.StartsWith(input));
                if (_currentSuggestion != null)
                {
                    _currentText = _currentSuggestion;
                    _selectionStart = input.Length;
                    _selectionLength = _currentSuggestion.Length - input.Length;

                    SuggestionBox.Text = _currentText;
                    SuggestionBox.Select(_selectionStart, _selectionLength);
                }
            }
            _currentInput = input;
        }
    }
}

The next step would be to convert this into a user control, so you can set your suggestions through bindings, however you handle that.

Adi Bradfield
  • 2,063
  • 1
  • 17
  • 27
  • Works like a charm. Thanks a lot. I had a quite similar way to solve it but i was trying to update the suggestion in the update function of the text dependency property which made me run into several problems when selecting the text. For some reason i never thought about using the text changed event. – Alexander Grasberger Aug 04 '18 at 14:20
  • @HansPetersburg When I saw this question, I thought it'd be that easy, and did exactly the same thing! Hence my comment about "fighting with WPF"! – Adi Bradfield Aug 04 '18 at 14:24
  • Yes, you are totaly right. I am very thankfull that you took your time to find a way around it. – Alexander Grasberger Aug 04 '18 at 15:01
  • Thank you so much, this is exactly what I needed! – Zer0 Aug 26 '20 at 07:02