【问题标题】:WPF Suggestion TextBoxWPF 建议文本框
【发布时间】:2019-01-12 01:39:39
【问题描述】:

我构建了一个小的 WPF 文本框来检查它的内容是否有效。现在我想实现提出建议的可能性。但不像互联网上的示例,其中会弹出一个带有建议的列表。我正在寻找一个示例,它可以像这样选择 TextBox:


如果有我可以查找的特定名称或您知道的任何示例代码,请告诉我。

【问题讨论】:

  • 嗯,有点不清楚。你要字典吗?还是您自己通过数据库或其他东西填写的建议列表?
  • 我做了类似的,但弹出显示带有文本框自动填充的建议列表
  • 是的,弹出窗口工作正常,但在那种情况下它们不符合我的需要,因为当时只有一个建议。除此之外,我还需要对每个看起来很奇怪的“单词”提出建议,但感谢你们的努力。

标签: c# wpf mvvm autocomplete


【解决方案1】:

在与 WPF 进行了很多斗争之后,我有一个适用于你的概念证明:

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;
        }
    }
}

下一步是将其转换为用户控件,因此您可以通过绑定设置建议,但您可以处理它。

【讨论】:

  • 像魅力一样工作。非常感谢。我有一个非常相似的方法来解决它,但我试图更新文本依赖属性的更新函数中的建议,这使我在选择文本时遇到了几个问题。出于某种原因,我从未想过使用文本更改事件。
  • @HansPetersburg 当我看到这个问题时,我认为它会那么简单,并且做了完全相同的事情!因此,我对“与 WPF 战斗”的评论!
  • 是的,你完全正确。非常感谢您花时间找到解决方法。
  • 非常感谢,这正是我所需要的!
【解决方案2】:

您可以通过稍微修改 here 的解决方案来使用水印(尤其是仅 xaml 的答案非常简短)。只需将水印文本设置为您的第一个建议。您可以添加一些功能,例如在代码隐藏中按退格键时删除建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多