Hi,
I have a CollectionView that have as ItemTemplate a ContentView.
I need to pass at the XAML of the ContentView a custom Command, so I have created a BindableProperty but it remains always null.
public ICommand ApriCommand
{
get { return (ICommand)GetValue(ApriCommandProperty); }
set { SetValue(ApriCommandProperty, value); }
}
public static readonly BindableProperty ApriCommandProperty = BindableProperty.Create(
propertyName: nameof(ApriCommand),
returnType: typeof(ICommand),
declaringType: typeof(MyItemTemplate),
defaultValue: null,
defaultBindingMode: BindingMode.OneWay,
propertyChanged: (BindableObject bindable, object oldValue, object newValue) =>
{
{
// never called
}
}
);
Then I have created the XAML for the item template:
<VerticalStackLayout Margin="0,0,0,2" >
<VerticalStackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference Me}, Path=ApriCommand}" CommandParameter="{Binding .}" />
</VerticalStackLayout.GestureRecognizers>
<Grid RowDefinitions="*" ColumnDefinitions="0.5*,0.5*" Padding="5" Background="LightSkyBlue" HeightRequest="60">
<Label
Grid.Column="0"
Text="{Binding ID}"
VerticalOptions="Center"
HorizontalOptions="Start" />
<Label
Grid.Column="1"
Text="{Binding Value}"
VerticalOptions="Center"
HorizontalOptions="End" />
</Grid>
<BoxView HeightRequest="1" Color="Black" VerticalOptions="End" />
</VerticalStackLayout>
Then I have created a command in the ViewModel and populate the list.
public partial class ViewModel : ObservableObject
{
[ObservableProperty]
private List<MyClass> _list;
[RelayCommand]
private async Task MyCommand(MyClass val)
{
// never called
await Application.Current.MainPage.DisplayAlert("Selected", val.Value, "ok");
}
public ViewModel()
{
List = [];
for (int i = 0; i < 30; i++)
{
List.Add(new() { ID = i, Value = $"I_{i}" });
}
}
}
Finally I have made the binding on the XAML page:
<ContentPage.BindingContext>
<local:ViewModel />
</ContentPage.BindingContext>
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<CollectionView ItemsSource="{Binding List}">
<CollectionView.ItemTemplate>
<DataTemplate>
<local:MyItemTemplate ApriCommand="{Binding MyCommandCommand}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</ScrollView>
But the propertyChanged event nor the MyCommand are never called.
How can I solve this?
Here a repo.
Thank you.