Can't get context of MenuItem in ListView ViewCell

Hi I have a ListView with a MunuItem defined XAML shown below:

<ListView x:Name="detailListView" Margin="0,30,0,0" ItemsSource="{Binding LVVD}" Header="" SeparatorColor="Gray"> <ListView.Behaviors> <behavior:ListViewSelectedItemBehavior Command="{Binding ListViewCommand}" Converter="{StaticResource SelectedItemConverter}" /> </ListView.Behaviors> <ListView.ItemTemplate> <DataTemplate> <ViewCell StyleId="disclosure-button"> <ViewCell.ContextActions> <MenuItem Text="Delete" IsDestructive="True" BindingContext="{Binding Source={x:Reference detailListView}, Path=BindingContext}" Command="{Binding MenuItemCommand}" CommandParameter="{Binding .}"/> </ViewCell.ContextActions> <ViewCell.View> ...

The idea here is to enable a removal of the Model item refered to in the ListView. I have defined a Command in my ViewModel shown here:

public ICommand MenuItemCommand { get; set; } MenuItemCommand = new Command(menuItemEvent); public void menuItemEvent() { Logic here to remove item from the list...? }

I'm new to Xamarin and MVVM so this is good and making some progress here. My problem is now I can't figure out how to get a reference to the data refered to in the ListView? I have tried changing the CommandParameters to ListView, ViewCell and others and get a cast exception. I read that using the {Binding .} allows access to the root context of the element, but in my situation it refers to my ViewModel page.

I added the MenuItem binding context as without it the binding context was not found (I read this is because of a bug with binding within a DataTemplate). So now in this situation how would I best accomplish my task?

Many thanks

Best Answer


  • RianDutraRianDutra ✭✭ USMember ✭✭

    @TripVoltage what is "MyCollection listData"?

    How can I get the selected item's id?

    public void menuItemEvent() {
    // Debug.WriteLine("Selected id: " + id);

Sign In or Register to comment.