【问题标题】:ExpandableListView can't expand list with where conditionExpandableListView 无法使用 where 条件展开列表
【发布时间】:2018-04-18 08:44:12
【问题描述】:

我的列表视图中有一个 .xaml 文件。我的列表与 MainViewModel 中的 Fournisseurs 绑定。当我使用 Button_Clicked 功能单击按钮 MoreDetail 时,我的列表会展开。但是现在,如果复选框为真,我添加了一个复选框,没问题我的列表是完整的,当我单击 MoreDetail 按钮时,我会显示所有信息。

但是当复选框为假时,我有一个列表,其中只有有效的fournisseurs(条件),但是当我单击更多详细信息时,我的项目不会展开。 我认为问题出在条件上。

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

.xaml

 <ContentPage.BindingContext>
         <local:MainViewModel />
    </ContentPage.BindingContext>
    <ListView   
                  x:Name="FournisseursListView"
                  ItemsSource="{Binding Fournisseurs}"
                  HasUnevenRows="True"
                  Margin="0,0,0,15"
                  ItemTapped="ListView_OnItemTapped"
                  BackgroundColor="White"
                >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="1" >
                            <StackLayout Orientation="Horizontal">
                                <StackLayout>
                                    <Button Image="{Binding .AfficheInfo}"
                                         Margin="4,1,0,0"
                                         BackgroundColor="Transparent"  
                                         BorderColor="White"   
                                         BorderWidth="0"   
                                         BorderRadius="0"
                                         WidthRequest="40" 
                                         HeightRequest="40"/>
                                </StackLayout>
                                <StackLayout WidthRequest="250"  Margin="10,0,0,0" Spacing="0" >
                                        <Label Text="{Binding .LibelleFournisseurFormated}"
                                       TextColor="Navy"
                                       FontSize="Default"/>
                                        <Label Text="{Binding .CodeFournisseur}"
                                       TextColor="Navy"
                                       FontSize="Default"/>
                                </StackLayout>
                                <StackLayout >
                                    <Button Image="{Binding .MoreDetail}"
                                         Clicked="Button_Clicked"
                                         Margin="0,5,0,0"
                                         BackgroundColor="Transparent"  
                                         BorderColor="White"   
                                         BorderWidth="0"   
                                         BorderRadius="0"
                                         WidthRequest="31" 
                                         HeightRequest="31"/>
                                 </StackLayout>
                            </StackLayout>

                            <StackLayout IsVisible="{Binding IsVisible}"
                                         Orientation="Vertical"
                                         Margin="10,5,0,0">
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding .CodeEntiteFournisseurFormated}" 
                                       TextColor="Navy"
                                       FontSize="Default"/>
                                    <Label Text="{Binding .ActifFournisseurFormated}" 
                                       TextColor="Navy"
                                       Margin="40,0,0,0"
                                       FontSize="Default"/>
                                    <Label Text="{Binding .EssoFournisseurFormated}" 
                                       TextColor="Navy"
                                       Margin="40,0,0,0"
                                       FontSize="Default"/>
                                </StackLayout>
                             </StackLayout>
                         </ViewCell>
                    </DataTemplate>
             </ListView.ItemTemplate>
       </ListView>
       <StackLayout Orientation="Horizontal">
            <controls:CheckBox 
                DefaultText="Afficher les fournisseurs inactifs" 
                Margin="0,0,0,15"
                CheckedChanged="CheckBox_CheckedChanged      

            />

      </StackLayout>

MainViewModel.cs

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);


        }

.xaml.cs

 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);


            }
         }

选中时:可以展开项目 List checked with an expand item 未选中时:单击时项目不展开 List unchecked, item doesn't expand on click

问题是:我可以在没有条件的情况下扩展列表,但是当我设置像 (where) 这样的条件时,我认为我的列表不会刷新并且她不会扩展。

【问题讨论】:

  • 有人吗?我认为这是刷新或列表问题,但我无法解决它

标签: c# listview xamarin xamarin.forms xamarin.android


【解决方案1】:

请使用以下代码。

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;
                        }
                    }

                }

            }
  1. 需要实现 INotifyPropertyChanged 事件的 IsVisible
  2. 如果搜索栏有测试,则在 listview 中点击方法获取搜索栏详细信息
  3. 在显示或隐藏方法中,请提供搜索列表数据的详细信息。
  4. 删除xmal代码行ItemsSource="{Binding Fournisseurs}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多