❔ How do I get event arguments and other data into my view model when using MVVM?

I'm using the Command interface in my XAML code on a TapGestureRecognizer ->
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:CataloguePageViewModel}},Path=TappedCommand}"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:CataloguePageViewModel}},Path=TappedCommand}"
I'm binding it to a method in my view model which allows me to change page when I select an item from a CollectionView ->
async Task Tapped(object obj)
{
await Shell.Current.GoToAsync(nameof(Views.CatalogueDetails));
}
async Task Tapped(object obj)
{
await Shell.Current.GoToAsync(nameof(Views.CatalogueDetails));
}
I know i can use a query parameter in the URL to pass that through to the new page but how do I send my currently selected item from the CollectionView in the View xaml to my View Model so I can populate the following details page with the correct information? My current assumption is that I bind to SelectedItem in the collection view in the view xaml but I'm not sure if this is the best approach. Thanks!
2 Replies
JakenVeina
JakenVeina13mo ago
My current assumption is that I bind to SelectedItem in the collection view in the view xaml but I'm not sure if this is the best approach.
it is, almost certainly
public class CataloguePageViewModel
{
public CatalogueViewModel SelectedCatalogue { get; set; }

public ICommand TappedCommand { get; }
}
public class CataloguePageViewModel
{
public CatalogueViewModel SelectedCatalogue { get; set; }

public ICommand TappedCommand { get; }
}
if the Tapped command is supposed to be "the selected catalog was tapped" in which case, I'd argue you should name it that way or really, move TappedCommand into CatalogueViewModel the whole setup seems a little weird to me, but I could just be getting the context wrong
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.