使TextBox建议筛选器不区分大小写



大家好,我使用了这篇文章中的代码,WPF Suggestion TextBox,来建议文本框上的文本。

像我想要的那样工作,但有一个问题,它是区分大小写的,我搜索了一个解决方案,但除了这行代码,什么也没找到;StringComparison.InvariantCultureIgnoreCase";但我不知道该把这个放在哪里。

我在帖子中使用的代码(完全相同,只是建议值不同(:

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

感谢您的帮助,

谢谢。

x.StartsWith(input, StringComparison.OrdinalIgnoreCase);

使用此选项可以忽略区分大小写,还可以改进其他部分,如_currentSuggestion

!input.Equals(_currentSuggestion, StringComparison.OrdinalIgnoreCase);

等等。。。

最新更新