我的Windows Phone应用程序有一个ListBox(由JSON填充)和一个用于搜索ListBox上项目的TextBox。
此代码工作正常,但我需要能够搜索“青金石”并找到“lápis”。 所以,我需要在搜索时忽略重音。
怎么做?
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (Items != null)
{
this.List1.ItemsSource = Items.Where(w => w.descricao.ToUpper().Contains(SearchTextBox.Text.ToUpper()));
}
}
private void WatermarkTB_GotFocus(object sender, RoutedEventArgs e)
{
if (SearchTextBox.Text == "Pesquisar Produto...")
{
SearchTextBox.Text = "";
SolidColorBrush Brush1 = new SolidColorBrush();
Brush1.Color = Colors.Red;
SearchTextBox.Foreground = Brush1;
}
}
答案 0 :(得分:3)
更改
w.descricao.ToUpper().Contains(SearchTextBox.Text.ToUpper())
要
CultureInfo.CurrentCulture.CompareInfo.IndexOf(
w.descricao,
SearchTextBox.Text,
CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase) != -1
IgnoreNonSpace
表示字符串比较必须忽略非间距组合字符,例如变音符号。
答案 1 :(得分:1)
扩展方法:
public static string RemoveDiacritic(this string text)
{
return text.Normalize(NormalizationForm.FormD).Where(chara => CharUnicodeInfo.GetUnicodeCategory(chara) != UnicodeCategory.NonSpacingMark).Aggregate<char, string>(null, (current, character) => current + character);
}
用法:
this.List1.ItemsSource = Items.Where(w => w.descricao.ToUpper().RemoveDiacritic().Contains(SearchTextBox.Text.ToUpper().RemoveDiacritic()));