So I have a collectionview and inside of that I have an expander with 3 buttons. The problem is that once I touch one of these 3 buttons they don't do nothing even if they're binded to commands.
Here's how it looks like:
Here's the code:
<CollectionView HorizontalScrollBarVisibility="Never"
Margin="0,5,20,15"
ItemsSource="{Binding Books}"
VerticalOptions="StartAndExpand"
ItemsLayout="HorizontalList"
HeightRequest="210">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Margin="8,0">
<xct:Expander>
<xct:Expander.Header>
<Grid RowDefinitions="*,auto">
<PanCake:PancakeView Grid.Row="0" Margin="0,0,20,0">
<ffimageloading:CachedImage Source="{Binding Cover}" HeightRequest="200" WidthRequest="145" Aspect="Fill"/>
</PanCake:PancakeView>
<Label Text="{Binding Title}" FontSize="14" Grid.Row="1" HorizontalOptions="Start" FontFamily="Inter" WidthRequest="145" LineBreakMode="TailTruncation" MaxLines="2" FontAttributes="Bold" TextColor="Black"/>
</Grid>
</xct:Expander.Header>
<xct:Expander.ContentTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" Margin="0,10,0,0">
<ImageButton Source="trash_24.png" BackgroundColor="Transparent" HorizontalOptions="Start" Command="{Binding RemoveBookFromCollection}" CommandParameter="{Binding .}" Margin="0,0,0,0">
</ImageButton>
<ImageButton Source="cross_24.png" BackgroundColor="Transparent" Command="{Binding SetBookAvailability}" CommandParameter="{Binding .}" WidthRequest="24" HeightRequest="24" HorizontalOptions="Start" Margin="10,0,0,0">
</ImageButton>
<ImageButton Source="check_24.png" BackgroundColor="Transparent" Command="{Binding SetBookAvailability}" CommandParameter="{Binding .}" HorizontalOptions="Start" Margin="10,0,0,0">
</ImageButton>
</StackLayout>
</DataTemplate>
</xct:Expander.ContentTemplate>
</xct:Expander>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>


The
DataTemplatehas a differentBindingContextthan the rest of the Page. TheDataTemplateinherits itsBindingContextfrom each book instance in theBookscollection. You need to use a relative binding in your command binding expression, e.g.:This will become apparent in the IDE, if you provide the
x:DataTypeto the DataTemplate.You also don't need the ContentTemplate for the body of the Expander, since you're directly providing the content yourself.
Since you didn't show the ViewModel, I will use a generic name, so you'll have to adapt it to your needs to make it work:
The
viewModel:YourViewModelshould of course be replaced with your actual namespace and ViewModel. The namespace needs to be included in the XAML header of your page as an XML namespace (xmlns) and ideally also provide thex:DataTypeto the Page in order to use compiled bindings which also helps the IDE in pointing out binding issues to you and it will produce compiler errors for faulty binding expressions, e.g.:Using the compiled bindings and the
x:DataTypeattribute is entirely optional, though, but will help with binding expressions. or similar.