I have what seems like a relatively simple problem, but I can't figure it out.
I have a Xamarin ListView, which is using an item template for the ViewCells as follows:
<ListView HasUnevenRows="True"
ItemsSource="{Binding Devices}"
ItemTemplate="{StaticResource DeviceDataTemplateSelector}"
SelectionMode="None"
ItemTapped="NewItemTapped"
/>
For now, 'DeviceDataTemplateSelector' always returns the following ViewCell (more ViewCells will be added later).
The ViewCell uses the Expander control from the Xamarin Community Toolkit
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:ViewModel="clr-namespace:App.ViewModels"
x:Class="App.UI.ViewCellStandardTemplate">
<ViewCell.View>
<!-- Calls an expander for each ViewCell. -->
<xct:Expander HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="{StaticResource UniversalMargin}"
>
<xct:Expander.Header>
<Grid HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="{StaticResource UniversalMargin}">
<Grid.RowDefinitions>
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="Header Text" />
</Grid>
</xct:Expander.Header>
<xct:Expander.Content>
<Grid HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="{StaticResource UniversalMargin}">
<Grid.RowDefinitions>
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0"
Grid.Column="0"
Text="Click Me"
Command="{Binding Source={RelativeSource AncestorType={x:Type ViewModel:PageViewModel}, Mode=FindAncestorBindingContext}, Path=ClickCommand}"
CommandParameter="{Binding .}"
/>
</Grid>
</xct:Expander.Content>
</xct:Expander>
</ViewCell.View>
</ViewCell>
So far all of this works correctly - the issue is that when I tap an item in the ListView, I want any other expanded items to collapse.
In other words, when I tap a ViewCell to expand it, I want all the other ViewCells in the ListView to set their respective Expander control's "IsExpanded" property to false.
I have tried calling the "ItemTapped" event in the ListView and then cycling through the elements in the ListView - I'm not sure if this is the right approach?
What is the best way to access the properties of the Expander control within the ViewCell?
I'm not sure how to go about this - any help would be much appreciated.
Thanks.