使文本框建议过滤器不区分大小写

Make TextBox Suggestion Filter Not Case Sensitive

大家好, 我使用此 post、 中的代码来建议文本框上的文本。

如我所愿,但有一个问题,它区分大小写,我搜索了一个解决方案,但除了这行代码“StringComparison.InvariantCultureIgnoreCase”之外什么也没找到,但我不知道该放在哪里这个。

我使用的 post 代码(完全相同,只是建议值不同):

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

等...