I have a page and it's binding context is set to SampleViewModel
. There I have a list of items (actually an ObservableCollection). Each of those items has an add to cart button. When clicked on that add to cart button,
I add that item to a new ObservableCollection as
CartItemsList
I update a bool property
item.HasAddedToCart = true
.And I increase
CartItemsCount
by 1.
Then after adding several items to the cart like that, I navigate to the Cart Page.
In the Cart Page. I need to show that CartItemsList
and the CartItemsCount
.
As my ViewModel (SampleViewModel
) already has those CartItemsList
and the CartItemsCount
I thought setting the Cart page's binding context to the same viewmodel instance. But when I set the binding context in XAML it creates a new instant of that viewmodel.
It should be possible to delete cart items from Cart page. So if I remove a cart item from Cart page and if I go back to the main page, that change should be visible in the main page too. So maintaining a CartItemsList
in the same viewmodel feels like the best approach.
But how to set the same vm instance as both those pages' binding context?