Hi,
I am implementing Expandable ListView in xamarin.forms,. But the listview showing all items expanded.
See my code below
public class MainListView
{
private Product _oldProduct;
public ObservableCollection<Product> Products { get; set; }
public MainListView()
{
Products = new ObservableCollection<Product>
{
new Product
{
Title = "Microsoft 650",
IsVisible = false
},
};
}
public void ShoworHiddenProducts(Product product)
{
if (_oldProduct == product)
{
product.IsVisible = !product.IsVisible;
UpDateProducts(product);
}
else
{
if (_oldProduct != null)
{
_oldProduct.IsVisible = false;
UpDateProducts(_oldProduct);
}
product.IsVisible = true;
UpDateProducts(product);
}
_oldProduct = product;
}
private void UpDateProducts(Product product)
{
var Index = Products.IndexOf(product);
Products.Remove(product);
Products.Insert(Index, product);
}
}
public class Product
{
public string Title { get; set; }
public bool IsVisible { get; set; }
}
Click event to open listview
private void ListViewItem_Tabbed(object sender, ItemTappedEventArgs e)
{
var product = e.Item as Product;
var vm = BindingContext as MainListView;
vm?.ShoworHiddenProducts(product);
}
This is xaml code
<ListView Margin="0,0,0,0"
VerticalOptions="FillAndExpand"
ItemTapped="ListViewItem_Tabbed"
ItemsSource="{Binding Products}"
HasUnevenRows="True"
BackgroundColor="Black">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="5">
<Label Text="{Binding Title}"
FontSize="Medium"
TextColor="Azure"/>
<StackLayout IsVisible="{Binding Isvisible}"
Orientation="Horizontal"
Margin="0,0,0,0">
<Button Text="Place Order"
WidthRequest="110"
FontSize="Medium"
BackgroundColor="Chocolate"
TextColor="White"/>
<Button Text="Details"
WidthRequest="110"
FontSize="Medium"
BackgroundColor="CornflowerBlue"
TextColor="DarkBlue"/>
<Button Text="Edit"
WidthRequest="110"
FontSize="Medium"
BackgroundColor="LightCoral"
TextColor="Maroon"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
For this implementation, I have followed below tutorial
https://www.c-sharpcorner.com/article/xamarin-forms-expendable-listview-app/