大家好,我使用這篇文章中的這段代碼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;
}
}
}
感謝任何幫助,
謝謝。
uj5u.com熱心網友回復:
x.StartsWith(input, StringComparison.OrdinalIgnoreCase);
您可以使用它忽略大小寫敏感,您可以改進其他部分,如 _currentSuggestion
!input.Equals(_currentSuggestion, StringComparison.OrdinalIgnoreCase);
等等...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437698.html
下一篇:異步任務阻塞UI
