The following is a snippet from my XAML:
<ListView.ItemTemplate>
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Basically, I am adding a trash can icon on every row and Binding the method DeleteCommand passing the parameter of the object on that row (which is actually a class for AddressType.
In my ViewModel, the following is where I wire up the commands. Snippet:
void DeleteCommandExecute(AddressType address)
{
if (IsBusy)
return;
IsBusy = true;
try
{
DataStore.DeleteAddressTypeAsync(address.Id);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
In my codebehind file, I set the BindingContext to the viewmodel
private AddressTypeViewModel viewModel;
public AddressTypes()
{
InitializeComponent();
BindingContext = viewModel = new AddressTypeViewModel();
stack = Addresspopup.PopupView.ContentTemplate.CreateContent() as StackLayout;
}
The DeleteCommand never executes. It appears no binding occured (even though the {Binding AddressTypeName} works as expected.
I have no idea why.
I also tried just catching the Tap event in my code behind by doing this in my XAML:
I added this to my code behind:
public void OnDelete_Tapped(object sender, EventArgs e)
{
viewModel.DeleteCommand.Execute(e); //deletes the item
viewModel.LoadAddressTypesCommand.Execute(true); //reload the addresstypes
}
The DeleteCommand does NOT execute! I put a breakpoint in that method in my view model - nothing happens - never hits it.
Can someone shed any light on this? Thanks in advance!