ListView - ContextMenu Command CommandParameter.

TobyKTobyK GBMember ✭✭✭
edited August 2016 in Xamarin.Forms

trying to stick to MVVM as much as possible using Xamarin Forms PCL porting to an Android deliverable - I have a TaskViewModel, with an ObservableCollection of ToDoItems. I also have a Tasks' View (bound to TaskViewModel) containing a listbox, with the ItemsSource bound to the ToDoItems.

I'm attempting to add a Context Menu Option to the ListView so when user selects and holds one of the ToDo items from within the ListView, the menu appears at the top (Android) with REMOVE option.

I have just now found out that any Command bound to the context menu are not recognized from the TaskViewModel but from the ToDoItem model instead?? So my issue is, how do I access the TaskViewModel.ToDoItems from the ToDoItem Model - as the TaskViewModel is the one that contains the collection where I will be able to go ToDoItems.Remove(SelectedToDoItem).

What is the best practice approach?

Best Answer

Answers

  • MattClayMattClay USMember

    try here: https://forums.xamarin.com/discussion/58709/context-actions-command-binding-with-parameter.

    Although I am getting iOS10 context menu errors at the moment.

  • TobyKTobyK GBMember ✭✭✭
    edited October 2016

    @MattClay Thanks for that - I read through the link you provided, wasn't aware binding could be done like that. Although having read some other binding examples, there's also an Ancestor type binding that could be used. However, I changed my approach and decided to add a SelectedListItem property that binds to the SelectedItem of the list. Then when users click on the SelectedListItem I pop up a navigation page with content bound to that, ie. SelectedListItem.Quantity. This way I keep the binding to the same object as where the commands are defined. So I've sorted that issue out, but now I'm facing another issue where I'm changing the quantity of the SelectedListItem, then press back to go back to the listview, but the listview doesn't reflect the new quantity until it is scrolled out of view, and the scrolled back into view... I putting it down to the fact I'm using the CachingStrategy = "True" setting, but could do with knowing what I need to do to get the right quantity shown immediately without having to scroll the item out and back into view again. Any suggestions?.... and yes, ListView is bound to observable collection, and the objects implement INotifyPropertyChanged.

  • MattClayMattClay USMember

    simply put, when you return to the list, the underlying collection and objects needs to be updated. Either refresh the data when you pop back, or pass back the updated quantity when you pop and directly update the collection. Personally I prefer hitting the DB again, it will pick up any other changes that may have occurred.

    If you have used MVVM and implemented property changed correctly, the view will automatically update when the data source does.

  • TobyKTobyK GBMember ✭✭✭
    edited October 2016

    If you have used MVVM and implemented property changed correctly, the view will automatically update when the data source does.

    That's what I thought. I shouldn't need to update the underlying objects, because I'm binding the view to the objects themselves. I'm expecting them to reflect the changes immediately, thus wondering if it is the CachingStrategy=true stopping the items in view being updated until scrolled off, etc.

    Just to add... If I update the quantity click back, quantity is back to original in the listview, if I then reselected the item, the quantity is back to what updated it too. The underlying objects are definately updated, its just the listview not reflecting the update.

  • TobyKTobyK GBMember ✭✭✭

    Lol. If I remove caching strategy="RecycleElement" it just doesn't update, even if scrolled out off and back into view.

  • MattClayMattClay USMember

    ah, its possible that when you say "Back" button this isn't being popped as I recall. I tended to navigate back to the list after the update. This forced the refresh of that list.

    As a thought, I tried to use the viewisappearing event to rebind. Honestly, it was a messy approach. You can raise a message (MessageCenter) as soon as the update occurs and viewisappearing can then subscribe to that message to refresh.

    I did get this working, but rolled it back, I didn't like the approach. Felt that it wasn't in the MVVM spirit... Happy to be corrected there.

  • TobyKTobyK GBMember ✭✭✭

    ah yeah, that is slightly different. I have my listview in page which has a navigator and I pushmodalasync over the top to allow users to edit the listview items.

  • MattClayMattClay USMember

    I had the same setup. Figured that once the edit was complete, the user workflow had finished and pushed back to the original list.

Sign In or Register to comment.