WPF建议文本框 [英] WPF Suggestion TextBox

查看:56
本文介绍了WPF建议文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个WPF文本框,用于检查其内容是否有效.现在,我想实现提出建议的可能性.但是不像互联网上出现的带有建议列表的示例.我正在寻找一个通过选择TextBox来做到这一点的示例:

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.

推荐答案

在与WPF进行了很多斗争之后,我得到了一个概念证明供您使用:

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

MainWindow.xaml

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:

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.

这篇关于WPF建议文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆