【问题标题】:How to ignore case when using TextBox to filter listview使用 TextBox 过滤列表视图时如何忽略大小写
【发布时间】:2020-06-06 11:48:16
【问题描述】:

有没有办法修改 TextBox 的 TextChanged 事件以忽略已输入的大小写?目前在我的ListView(包含城镇名称)和TextBox 中,我想搜索我想要的任何城镇名称,而不必使用大写字母。

private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
         {
             if (string.IsNullOrEmpty(txtSearch.Text))
             {
                 this.ListTowns.ItemsSource = this.listItemTowns;
             }
             this.ListTowns.ItemsSource = this.listTowns.Where((item) => { return item.TownTitle.Contains(txtSearch.Text); });
         }

【问题讨论】:

    标签: c# windows uwp


    【解决方案1】:

    这应该可行:

    private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (string.IsNullOrEmpty(txtSearch.Text))
        {
            this.ListTowns.ItemsSource = this.listItemTowns;
        }
        else
        {
            this.ListTowns.ItemsSource = this.listTowns.Where(item => item.TownTitle.Contains(txtSearch.Text, StringComparison.OrdinalIgnoreCase));
        }
    }
    

    【讨论】:

      【解决方案2】:

      以下应该可以解决问题:

      private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
      {
                   if (string.IsNullOrEmpty(txtSearch.Text))
                   {
                       this.ListTowns.ItemsSource = this.listItemTowns;
                   }
                   this.ListTowns.ItemsSource = this.listTowns.Where((item) => { return item.TownTitle.ToLower().Contains(txtSearch.Text.ToLower()); });
      }
      

      您可以将字符串转换为 .ToLower() 或 .ToUpper()。

      保重

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多