Unable to bind a ContextAction menuItem command to the viewModel.

ChrisVardonChrisVardon ✭✭GBMember ✭✭
edited February 2017 in Xamarin.Forms

I have a ListView with Textcells and have added TextCell.ContextActions with MenuItems. I have a Delete Menu item and bounf the command to a command in my view model which is hooked up to a method. However, the method is not being hit. Any ideas?

<ListView x:Name="lvNotes" ItemsSource="{Binding Notes}" IsVisible="{Binding PersonNotesVisible}" RowHeight="70"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Title}" Detail="{Binding DateAdded}"> <TextCell.ContextActions> <MenuItem Text="Share"/> <MenuItem Command="{Binding DeleteNoteCommand}" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" /> </TextCell.ContextActions> </TextCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

In the VIEWMODEL, I have initiated the DeleteNoteCommand in the VM constructor

public ICommand DeleteNoteCommand { get; set; }
DeleteNoteCommand = new Command(DeleteAction);

    private void DeleteAction(object obj)


Best Answer


  • ChrisVardonChrisVardon ✭✭ GBMember ✭✭

    Hey @ionixjunior ,

    That was a great explanation and your suggestions worked wonderfully. Thanks you very much!


  • ManInTheLoftManInTheLoft ✭✭ GBBeta, University ✭✭

    @ionixjunior - worked for me too, thanks.


  • slawrenceslawrence Member

    @ionixjunior - Awesome explanation, worked for me as well. Nice work!

  • DavyHomesDavyHomes ✭✭ Member ✭✭

    @ionixjunior said:
    Hi @ChrisVardon, your ListView is binding your Notes property on your view model, however the content inside DateTemplate of the ListView don't see directly your view model, because he is binded with your Note object.

    For this, you need perform view to view binding. See this https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/#View-to-View_Bindings

    My suggestion for you:
    1) In your XAML, add a name for reference later:


    2) In your MenuItem refer BindingContext of your page for assign your command:

    <MenuItem Command="{Binding Path=BindingContext.DeleteNoteCommand, Source={x:Reference YourView}}" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" />

    I hope this help you.

    I have a custom ViewCell which i select by a dataTemplateselector. How can i reference the view in there ?

Sign In or Register to comment.