UWP - 搜索 gridview 而不管大写或小写字母
UWP - search the gridview regardless of uppercase or lowercase letters
我有一个 gridview,其中包含文件夹中的文件并添加了搜索功能。我在搜索的时候遇到了一个问题,就是如果文件名中有一个大字母,但是我用小写字母输入,那么就找不到了。
如下图:
使用小写
使用大写
如何克服,让搜索结果不关心小写字母和大写字母?
代码:
ObservableCollection<Book> datasource = new ObservableCollection<Book>();
private void SearchText_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(searchText.Text))
{
this.itemGridView.ItemsSource = this.datasource.Where((item) => { return item.Name.Contains(searchText.Text); });
}
else
{
this.itemGridView.ItemsSource = datasource;
}
}
Book.cs:
public class Book
{
public string Name { get; set; }
public string Direktori { get; set; }
public ImageSource Image { get; set; }
}
向 Contains()
方法添加一个 StringComparison
参数。所以像这样:
return item.Name.Contains(searchText.Text, StringComparison.OrdinalIgnoreCase);
我有一个 gridview,其中包含文件夹中的文件并添加了搜索功能。我在搜索的时候遇到了一个问题,就是如果文件名中有一个大字母,但是我用小写字母输入,那么就找不到了。 如下图:
使用小写
使用大写
如何克服,让搜索结果不关心小写字母和大写字母?
代码:
ObservableCollection<Book> datasource = new ObservableCollection<Book>();
private void SearchText_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(searchText.Text))
{
this.itemGridView.ItemsSource = this.datasource.Where((item) => { return item.Name.Contains(searchText.Text); });
}
else
{
this.itemGridView.ItemsSource = datasource;
}
}
Book.cs:
public class Book
{
public string Name { get; set; }
public string Direktori { get; set; }
public ImageSource Image { get; set; }
}
向 Contains()
方法添加一个 StringComparison
参数。所以像这样:
return item.Name.Contains(searchText.Text, StringComparison.OrdinalIgnoreCase);