CommunityToolkit / CommunityToolkit/MVVM-Samples

Button command in DataTemplate NOT working in Android mobile app

Open
#139 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
1.4k
Forks
265
PR merge metrics
No merged PRs in 30d

Description

Hi,
I have a button in one page which works using markup with either these lines of code
1) .Bind(Button.CommandProperty, nameof(DeliveryCollectionDetailsPageViewModel.CompleteOrderCommand))
2) .BindCommand(static (DeliveryCollectionDetailsPageViewModel vm) => vm.CompleteOrderCommand, mode: BindingMode.OneWay)

I have another page with a CollectionView who's template I define in another file under Views which contains a button.
The above 2 lines don't work in the DataTemplate and neither di the following.

3) .Invoke(cv => {
var tapGesture = new TapGestureRecognizer();
tapGesture.SetBinding(TapGestureRecognizer.CommandProperty, "TakeRightPictureForConsignmentCommand");
cv.GestureRecognizers.Add(tapGesture);
})
4).BindTapGesture( (ConsignmentPicturesViewModel vm) => vm.TakeRightPictureForConsignmentCommand)

5).Bind(Button.CommandProperty, getter: new Binding("TakeRightPictureForConsignmentCommand", source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(ConsignmentPicturesViewModel))))

6).Bind(
Button.CommandProperty,getter: (ConsignmentPicturesViewModel vm)=>vm.TakeRightPictureForConsignmentCommand,
new Binding("TakeRightPictureForConsignmentCommand",
source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor,
typeof(DeliveryCollectionDetailsPageViewModel)))

7) .Bind(
Button.CommandProperty,getter: nameof(ConsignmentPicturesViewModel.TakeRightPictureForConsignmentCommand)),
new Binding("TakeRightPictureForConsignmentCommand",
source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor,
typeof(DeliveryCollectionDetailsPageViewModel)))

Before I give up and re-write the page in Xaml, do you have any suggestions?

Thanks! 😊

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.