How to work with expandable multi-level Xamarin.Forms ListView (SfListView)?
You can expand or collapse a multi-level ListView in Xamarin.Forms. Refer to the document to work with the multi-level Xamarin.Forms ListView from here.
XAML
Initially hide the inner ListViews and expand or collapse on interaction by handling the Visibility.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ListViewSample"
xmlns:listView="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
xmlns:dataSource="clr-namespace:Syncfusion.DataSource;assembly=Syncfusion.DataSource.Portable"
x:Class="ListViewSample.NestedListView">
<ContentPage.Content>
<Grid>
<listView:SfListView x:Name="outerListView" ItemsSource="{Binding ContactInfo}" AutoFitMode="DynamicHeight">
<listView:SfListView.ItemTemplate>
<DataTemplate>
<Grid BackgroundColor="#f4f4f2" >
...
<Button Text="^" Rotation="{Binding IsInnerListVisible, Converter={StaticResource rotationConverter}}" Command="{Binding Path=BindingContext.OuterListTapCommand, Source={x:Reference outerListView}}" CommandParameter="{Binding .}" TextColor="WhiteSmoke" BackgroundColor="Transparent" FontAttributes="Bold" FontSize="20" Padding="10,0,0,0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Label Text="{Binding Location}" Grid.Column="1" Padding="10,0,0,0" VerticalOptions="Center" FontAttributes="Bold"/>
</Grid>
<listView:SfListView x:Name="innerListView" Margin="5" Grid.Row="1" IsScrollingEnabled="False" IsVisible="{Binding IsInnerListVisible}" ItemsSource="{Binding ContactDetails}" AutoFitMode="DynamicHeight">
<listView:SfListView.ItemTemplate>
<DataTemplate>
<Grid x:Name="grid" RowSpacing="1" BackgroundColor="#bbbfca">
...
<Button Text="{Binding IsSubListVisible, Converter={StaticResource textConverter}}" Command="{Binding Path=BindingContext.InnerListTapCommand, Source={x:Reference innerListView}}" CommandParameter="{Binding .}" BackgroundColor="Transparent" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" FontSize="20" Grid.Column="2" />
</Grid>
<listView:SfListView x:Name="SubListView" IsVisible="{Binding IsSubListVisible}" Margin="5" ItemsSource="{Binding Members}" Grid.Row="1" HeightRequest="{Binding Converter={StaticResource heightConverter}, ConverterParameter={x:Reference SubListView}}" ItemSize="50" BackgroundColor="#f4f4f2">
<listView:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="10,0,0,0">
<Label Text="{Binding Name}" VerticalOptions="CenterAndExpand"/>
</Grid>
</DataTemplate>
</listView:SfListView.ItemTemplate>
</listView:SfListView>
</Grid>
</DataTemplate>
</listView:SfListView.ItemTemplate>
</listView:SfListView>
</Grid>
</DataTemplate>
</listView:SfListView.ItemTemplate>
</listView:SfListView>
</Grid>
</ContentPage.Content>
</ContentPage>
C#
Change the visibility property that is bounded to the inner level ListViews.
namespace ListViewSample
{
public class NestedListViewViewModel : INotifyPropertyChanged
{
public Command<object> OuterListTapCommand { get; set; }
public NestedListViewViewModel()
{
OuterListTapCommand = new Command<object>(OnOuterListTapped);
}
private void OnOuterListTapped(object obj)
{
var item = obj as ContactInfo_NestedListView;
item.IsInnerListVisible = !item.IsInnerListVisible;
}
}
public class ContactInfo_NestedListView : INotifyPropertyChanged
{
private bool isInnerListVisible;
public Command InnerListTapCommand { get; set; }
public bool IsInnerListVisible
{
get { return isInnerListVisible; }
set
{
isInnerListVisible = value;
this.RaisedOnPropertyChanged("IsInnerListVisible");
}
}
public ContactInfo_NestedListView(int CountMax)
{
InnerListTapCommand = new Command(OnInnerListTapped);
}
private void OnInnerListTapped(object obj)
{
var item = obj as DetailsContactInfo;
item.IsSubListVisible = !item.IsSubListVisible;
}
}
public class DetailsContactInfo:INotifyPropertyChanged
{
private bool isSubListVisible;
public bool IsSubListVisible
{
get { return isSubListVisible; }
set
{
isSubListVisible = value;
this.RaisedOnPropertyChanged("IsSubListVisible");
}
}
}
}
Conclusion
I hope you enjoyed learning about how to work with expander multilevel in Xamarin.Forms ListView (SfListView).
You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!