C# 如何从代码隐藏(MVVM)中使用搜索栏筛选ListView

C# 如何从代码隐藏(MVVM)中使用搜索栏筛选ListView,c#,xaml,xamarin,mvvm,xamarin.forms,C#,Xaml,Xamarin,Mvvm,Xamarin.forms,我有一个列表视图,你想用搜索栏过滤它 目前,该列表由my ViewModel的构造函数(参数步骤)填写,如下所示 //AGREGAR-SUSTANCIA视图模型 List<AgregarSustancia> listaSustanciasAux; ObservableCollection<AgregarSustancia> listaSustancias; public ObservableCollection<AgregarSu

我有一个列表视图,你想用搜索栏过滤它

目前,该列表由my ViewModel的构造函数(参数步骤)填写,如下所示

//AGREGAR-SUSTANCIA视图模型

    List<AgregarSustancia> listaSustanciasAux;

     ObservableCollection<AgregarSustancia> listaSustancias;

      public ObservableCollection<AgregarSustancia> ListaSustancias
      {
           get
           {
              return listaSustancias;
           }
           set
           {
             if (listaSustancias != value)
             {
                  listaSustancias = value;
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ListaSustancias)));
             }
          }
        }

         public AgregarSustanciaViewModel(ObservableCollection<AgregarSustancia> listaAgregarSustancia)
         {

               ListaSustancias = new ObservableCollection<AgregarSustancia>();
               listaSustanciasAux = new List<AgregarSustancia>();

               listaSustanciasAux = listaAgregarSustancia.ToList();
               ListaSustancias = listaAgregarSustancia;
         }

public ICommand SearchCommand
        {
            get
            {
                return new RelayCommand(Search);
            }
        }

        void Search()
        {

                if (string.IsNullOrEmpty(Filter))
                {
                    ListaSustancias = new ObservableCollection<AgregarSustancia>(listaSustanciasAux.OrderBy(o => o.NombreSustancia).OrderByDescending(r => r.IdSustancia));
                }
                else
                {
                    ListaSustancias = new ObservableCollection<AgregarSustancia>(listaSustanciasAux.Where(c => c.IdSustancia.ToString().ToLower().
                                       Contains(Filter.ToLower()) || c.NombreSustancia.ToLower().RemoveDiacritics().Contains(Filter.ToLower())).OrderByDescending(r => r.IdSustancia));
                }

        }
如下图所示,我捕获了在搜索栏中输入的文本

但是我如何访问我的列表,然后通过linq进行过滤呢?如何从ViewModel的绑定列表后面的代码访问

在搜索栏中输入字符时,如何筛选列表?
有什么帮助吗?

首先,你必须在你的项目中添加行为。

然后,您可以这样使用:

<SearchBar>
            <SearchBar.Behaviors>
                <behaviour:EventToCommandBehavior EventName="TextChanged" Command=" {Binding TextChangedCommand}" />
            </SearchBar.Behaviors>
</SearchBar>



  public ICommand TextChangedCommand => new Command((o) =>
        {
            if (o is TextChangedEventArgs text)
            {
            //do some works
            }
        });

公共ICommand TextChangedCommand=>new命令((o)=>
{
如果(o为文本更改格式文本)
{
//做一些工作
}
});
如果不想使用行为,也可以使用SearchCommand

<SearchBar x:Name="searchBar"   
                       SearchCommand="{Binding MyFilterCommand}"
                       SearchCommandParameter="{Binding Text, Source={x:Reference searchBar}}"/>


您的代码隐藏应该有一个对VM的引用。只需在VM中创建一个方法来筛选并从代码隐藏中调用它。我已经编辑了我的问题,添加了从ViewModel中筛选列表的方法,但是如何从代码隐藏中调用它?我怎样做你所指的参考资料?有什么帮助吗@Jasonhow you's creating your VM?@CristofherAmbiado Hi,您可以参考以检查问题所在。当按如下方式筛选列表时,我的应用程序被删除,命令中应用的强制转换不起作用,有关如何使用“O”参数listastancias=new observetecollection(listastanciasaux.where)的一些帮助(c=>c.IdSustancia.ToString().ToLower().Contains((字符串)o.ToLower())| | c.NombreSustancia.ToLower().RemoveDiacritics().Contains((字符串)o.ToLower()).OrderByDescending(r=>r.IdSustancia))
o.NewTextValue
-请参阅@Cristofher Ambiado我回答了您上面的问题。您必须强制转换“o”参数。这是一种强制转换方法:如果(o是textchangedventargs text),现在您可以将“text”用作强制转换的o。
<SearchBar>
            <SearchBar.Behaviors>
                <behaviour:EventToCommandBehavior EventName="TextChanged" Command=" {Binding TextChangedCommand}" />
            </SearchBar.Behaviors>
</SearchBar>



  public ICommand TextChangedCommand => new Command((o) =>
        {
            if (o is TextChangedEventArgs text)
            {
            //do some works
            }
        });
<SearchBar x:Name="searchBar"   
                       SearchCommand="{Binding MyFilterCommand}"
                       SearchCommandParameter="{Binding Text, Source={x:Reference searchBar}}"/>