WPF 建议文本框

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


http://img2.mukewang.com/6190d7430001f44b02130071.jpg如果有我可以查找的特定名称或您知道的任何示例代码,请告诉我。

幕布斯6054654
浏览 243回答 2
2回答

拉莫斯之舞

经过与 WPF 的大量斗争,我有一个为您工作的概念证明:主窗口.xaml<Window x:Class="Solutions.MainWindow"&nbsp; &nbsp; &nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; &nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; &nbsp; &nbsp; mc:Ignorable="d"&nbsp; &nbsp; &nbsp; &nbsp; Title="MainWindow" Height="450" Width="800">&nbsp; &nbsp; <Grid>&nbsp; &nbsp; &nbsp; &nbsp; <TextBox VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="SuggestionBox" Width="200"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/>&nbsp; &nbsp; </Grid></Window>主窗口.xaml.cs:using System.Linq;using System.Windows;using System.Windows.Controls;namespace Solutions{&nbsp; &nbsp; public partial class MainWindow : Window&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private static readonly string[] SuggestionValues = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "England",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "USA",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "France",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Estonia"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; public MainWindow()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SuggestionBox.TextChanged += SuggestionBoxOnTextChanged;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private string _currentInput = "";&nbsp; &nbsp; &nbsp; &nbsp; private string _currentSuggestion = "";&nbsp; &nbsp; &nbsp; &nbsp; private string _currentText = "";&nbsp; &nbsp; &nbsp; &nbsp; private int _selectionStart;&nbsp; &nbsp; &nbsp; &nbsp; private int _selectionLength;&nbsp; &nbsp; &nbsp; &nbsp; private void SuggestionBoxOnTextChanged(object sender, TextChangedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var input = SuggestionBox.Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input.Length > _currentInput.Length && input != _currentSuggestion)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _currentSuggestion = SuggestionValues.FirstOrDefault(x => x.StartsWith(input));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (_currentSuggestion != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _currentText = _currentSuggestion;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _selectionStart = input.Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _selectionLength = _currentSuggestion.Length - input.Length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SuggestionBox.Text = _currentText;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SuggestionBox.Select(_selectionStart, _selectionLength);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _currentInput = input;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}下一步是将其转换为用户控件,以便您可以通过绑定设置您的建议,但您可以自行处理。

HUX布斯

您可以通过稍微修改此处的解决方案来使用水印(尤其是仅 xaml 的答案非常简短)。只需将水印文本设置为您的第一个建议即可。您可以添加一些功能,例如在代码隐藏中按退格键时删除建议。
打开App,查看更多内容
随时随地看视频慕课网APP