Hi MarkConnelly-6630,
Welcome to our Microsoft Q&A platform!
As alessandrocaliaro said, you can use relative bindings to access the viewmodel(BindingContext) of this Page.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:deleteitem"
x:Class="deleteitem.MainPage"
x:Name="ListviewPage">
<ContentPage.BindingContext>
<local:MainPageViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<ListView x:Name="listView" ...>
<ListView.ItemTemplate>
...
<ImageButton Source="deleteicon.png" VerticalOptions="Center" HorizontalOptions="Start" Grid.Row="0" Grid.Column="2" Padding="10"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainPageViewModel}}, Path=DeleteCommand}"
CommandParameter="{Binding .}"></ImageButton>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
And then declare the DeleteCommand in the viewmodel.
class MainPageViewModel
{
public ObservableCollection<Basket> BasketList { get; set; }
public ICommand DeleteCommand { get; }
public MainPageViewModel()
{
BasketList = new ObservableCollection<Basket>();
BasketList.Add(...);
DeleteCommand = new Command<Basket>(DeleteItem);
}
public void DeleteItem(Basket basket)
{
var deletedItem = basket as Basket;
if (basket == null)
return;
BasketList.Remove(basket);
}
}
Another way, you can also use x:Reference.
<ImageButton Source="deleteicon.png" VerticalOptions="Center" HorizontalOptions="Start" Grid.Row="0" Grid.Column="2" Padding="10"
Command="{Binding Source={x:Reference Name=ListviewPage}, Path=BindingContext.DeleteCommand}"
CommandParameter="{Binding .}"></ImageButton>
Regards,
Kyle
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.