C# 可扩展列表视图可以';t使用where条件展开列表

C# 可扩展列表视图可以';t使用where条件展开列表,c#,listview,xamarin,xamarin.forms,xamarin.android,C#,Listview,Xamarin,Xamarin.forms,Xamarin.android,我的listview中有一个.xaml文件。我的列表与我的MainViewModel中的Fourniseurs绑定。当我使用功能单击按钮时,我的列表将展开。但现在,我添加了一个复选框,如果该复选框为true,则不会出现问题。我的列表已完成,当我单击MoreDetail按钮时,我会显示所有信息 但是当复选框为false时,我有一个只有valids Fourniseurs(where条件)的列表,但是当我单击MoreDetail时,我的项目不会展开。 我认为问题来自于这种状况 如果你愿意,我可以在G

我的listview中有一个.xaml文件。我的列表与我的MainViewModel中的Fourniseurs绑定。当我使用功能单击按钮时,我的列表将展开。但现在,我添加了一个复选框,如果该复选框为true,则不会出现问题。我的列表已完成,当我单击MoreDetail按钮时,我会显示所有信息

但是当复选框为false时,我有一个只有valids Fourniseurs(where条件)的列表,但是当我单击MoreDetail时,我的项目不会展开。 我认为问题来自于这种状况

如果你愿意,我可以在GitHub上共享我的代码

.xaml



请使用下面的代码

private bool? _IsVisible;
    public bool? IsVisible
    {
        get { return _IsVisible; }
        set {
            _IsVisible = value;
            if(_IsVisible !=null)
            {
                OnPropertyChanged();

            }
        }

    }
     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {

                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }

    private void UpdateProducts(Product product)
            {

                if(searchProducts != null && searchProducts.Count > 0)
                {
                    var index = searchProducts.IndexOf(product);
                    searchProducts.Remove(product);
                    searchProducts.Insert(index, product);
                }
                else
                {
                    var index = Products.IndexOf(product);
                    Products.Remove(product);
                    Products.Insert(index, product);
                }

            }



    private void ListView_OnItemTapped(object sender, ItemTappedEventArgs e)
            {
                var product = e.Item as Product;
                var vm = BindingContext as MainViewModel;

                if (string.IsNullOrEmpty(searchbar.Text))
                {
                    vm.searchProducts = new ObservableCollection<Product>();
                }

                vm?.ShowOrHidePoducts(product);
            }






    private void onsearchchanged(object sender ,TextChangedEventArgs e)
            {
                var vm = BindingContext as MainViewModel;

                if (string.IsNullOrEmpty(e.NewTextValue))
                    windowslistview.ItemsSource = vm?.Products;
                else
                {
                    if (!string.IsNullOrEmpty(e.NewTextValue))
                    {

                        var detailsdata = vm?.Products.Where(i =>
                   i.Name.ToLower().Contains(e.NewTextValue.ToLower())).ToList();

                        if(detailsdata !=null && detailsdata.Count> 0)
                        {
                            ObservableCollection<Product> myCollection = new ObservableCollection<Product>(detailsdata as List<Product>);
                            vm.searchProducts = myCollection;
                            windowslistview.ItemsSource = detailsdata;
                        }
                        else
                        {
                            windowslistview.ItemsSource = vm?.Products;
                        }
                    }

                }

            }
private bool_可见;
公共场所?可见
{
获取{return\u IsVisible;}
设置{
_IsVisible=值;
如果(_IsVisible!=null)
{
OnPropertyChanged();
}
}
}
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
私有void UpdateProducts(产品)
{
if(searchProducts!=null&&searchProducts.Count>0)
{
var指数=searchProducts.IndexOf(产品);
搜索产品。删除(产品);
搜索产品。插入(索引,产品);
}
其他的
{
var指数=产品。IndexOf(产品);
产品。移除(产品);
产品。插入(索引,产品);
}
}
私有无效列表视图已映射(对象发送方,ItemTappedEventArgs e)
{
var product=e.作为产品的项目;
var vm=BindingContext作为MainViewModel;
if(string.IsNullOrEmpty(searchbar.Text))
{
vm.searchProducts=新的ObservableCollection();
}
vm?显示或隐藏产品(产品);
}
SearchChanged上的私有void(对象发送方,textchangedventargs e)
{
var vm=BindingContext作为MainViewModel;
if(string.IsNullOrEmpty(e.NewTextValue))
windowslistview.ItemsSource=vm?.Products;
其他的
{
如果(!string.IsNullOrEmpty(e.NewTextValue))
{
var detailsdata=vm?.Products.Where(i=>
i、 Name.ToLower().Contains(例如NewTextValue.ToLower()).ToList();
if(detailsdata!=null&&detailsdata.Count>0)
{
ObservableCollection myCollection=新的ObservableCollection(详细数据如列表所示);
vm.searchProducts=myCollection;
windowslistview.ItemsSource=detailsdata;
}
其他的
{
windowslistview.ItemsSource=vm?.Products;
}
}
}
}
  • 需要实现INotifyPropertyChanged事件的IsVisible
  • 在listview中,如果搜索栏有测试或没有测试,则获取搜索栏详细信息
  • 在showorhide方法中,请提供搜索列表数据的详细信息
  • 删除xmal代码行
    ItemsSource=“{Binding fourniseurs}”

  • 任何人我认为这是一个刷新或列表问题,但我无法解决它
    public MainViewModel() 
        {
    
    
    
            Fournisseurs = new ObservableCollection<Fournisseur>
    
            {
                new Fournisseur
                {
                    CodeFournisseur = "100001A",
                    LibelleFournisseur = "L'AGENCE OUATE",
                    CodeEntiteFournisseur = "GCT",
                    Adresse1Fournisseur = "100 RUE DU COLONNEL MOLL",
                    Adresse2Fournisseur = "",
                    CPFournisseur = "75017",
                    VilleFournisseur = "PARIS",
                    PaysFournisseur = "France",
                    TelephoneFournisseur = "02.28.09.03.00",
                    FaxFournisseur = "02.28.09.03.09",
                    EmailFournisseur = "",
                    BoolActifFournisseur = true,
                    BoolEssoFournisseur = true,
                    IsVisible = false,
                },
                new Fournisseur
                {
                    CodeFournisseur = "100001A",
                    LibelleFournisseur = "L'AGENCE OUATE",
                    CodeEntiteFournisseur = "GCT",
                    Adresse1Fournisseur = "100 RUE DU COLONNEL MOLL",
                    Adresse2Fournisseur = "",
                    CPFournisseur = "75017",
                    VilleFournisseur = "PARIS",
                    PaysFournisseur = "France",
                    TelephoneFournisseur = "02.28.09.03.00",
                    FaxFournisseur = "02.28.09.03.09",
                    EmailFournisseur = "",
                    BoolActifFournisseur = false,
                    BoolEssoFournisseur = true,
                    IsVisible = false,
                }
          };
    
         public void ShowOrHideDetailFournisseur(Fournisseur Fournisseur)
            {
                if (_oldFournisseur == Fournisseur)
                {
                    // click twice on the same item will hide it
                    Fournisseur.IsVisible = !Fournisseur.IsVisible;
                    UpdateFournisseurs(Fournisseur);
    
                }
                else
                {
                    if (_oldFournisseur != null)
                    {
                        // hide previous selected item
                        _oldFournisseur.IsVisible = false;
                        UpdateFournisseurs(_oldFournisseur);
                    }
                    // show selected item
                    Fournisseur.IsVisible = true;
                    UpdateFournisseurs(Fournisseur);
                }
    
                _oldFournisseur = Fournisseur;
            }
    
    
    
            private void UpdateFournisseurs(Fournisseur Fournisseur)
            {
    
                var index = Fournisseurs.IndexOf(Fournisseur);
                Fournisseurs.Remove(Fournisseur);
                Fournisseurs.Insert(index, Fournisseur);
    
    
            }
    
     private void Button_Clicked(object sender, EventArgs e)
            {
                var button = sender as Button;
                var fournisseur = button.BindingContext as Fournisseur;
                var vm = BindingContext as MainViewModel;
                vm?.ShowOrHideDetailFournisseur(fournisseur);
            }
    
    private void CheckBox_CheckedChanged(object sender, XLabs.EventArgs<bool> e)
            {
                var checkbox = sender as CheckBox;
                var vm = BindingContext as MainViewModel;
    
    
                if (checkbox.Checked == true) 
                {
    
                    FournisseursListView.ItemsSource = vm.Fournisseurs;
                 }
                else
                {                 
                    FournisseursListView.ItemsSource = vm.Fournisseurs.Where(x=> x.BoolActifFournisseur == true);
    
    
                }
             }
    
    private bool? _IsVisible;
        public bool? IsVisible
        {
            get { return _IsVisible; }
            set {
                _IsVisible = value;
                if(_IsVisible !=null)
                {
                    OnPropertyChanged();
    
                }
            }
    
        }
         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
                {
    
                    PropertyChangedEventHandler handler = PropertyChanged;
                    if (handler != null)
                    {
                        handler(this, new PropertyChangedEventArgs(propertyName));
                    }
                }
    
        private void UpdateProducts(Product product)
                {
    
                    if(searchProducts != null && searchProducts.Count > 0)
                    {
                        var index = searchProducts.IndexOf(product);
                        searchProducts.Remove(product);
                        searchProducts.Insert(index, product);
                    }
                    else
                    {
                        var index = Products.IndexOf(product);
                        Products.Remove(product);
                        Products.Insert(index, product);
                    }
    
                }
    
    
    
        private void ListView_OnItemTapped(object sender, ItemTappedEventArgs e)
                {
                    var product = e.Item as Product;
                    var vm = BindingContext as MainViewModel;
    
                    if (string.IsNullOrEmpty(searchbar.Text))
                    {
                        vm.searchProducts = new ObservableCollection<Product>();
                    }
    
                    vm?.ShowOrHidePoducts(product);
                }
    
    
    
    
    
    
        private void onsearchchanged(object sender ,TextChangedEventArgs e)
                {
                    var vm = BindingContext as MainViewModel;
    
                    if (string.IsNullOrEmpty(e.NewTextValue))
                        windowslistview.ItemsSource = vm?.Products;
                    else
                    {
                        if (!string.IsNullOrEmpty(e.NewTextValue))
                        {
    
                            var detailsdata = vm?.Products.Where(i =>
                       i.Name.ToLower().Contains(e.NewTextValue.ToLower())).ToList();
    
                            if(detailsdata !=null && detailsdata.Count> 0)
                            {
                                ObservableCollection<Product> myCollection = new ObservableCollection<Product>(detailsdata as List<Product>);
                                vm.searchProducts = myCollection;
                                windowslistview.ItemsSource = detailsdata;
                            }
                            else
                            {
                                windowslistview.ItemsSource = vm?.Products;
                            }
                        }
    
                    }
    
                }