Forum Xamarin.Forms

Is it MVVM friendly to use other ViewModels as bindable properties?

I have a need where I have 3 nested "generations" in a relational database, that I have to include in one View.

When I use nested lists in the view, and that's triple nesting I am talking about...

...I am forced to move some Commands from the ViewModel into the Model classes.

I want to avoid this at all cost, as it's not MVVM friendly.

So that's why I am asking is it MVVM friendly to use other ViewModels as bindable properties, like in the following Example?


public class MainViewModel : FreshBasePageModel 
    public ObservableCollection<OtherViewModel> OtherCollection { get; set; }

public class OtherViewModel: FreshBasePageModel 
    public Command SomeCommand { get; set; }

And use it like this in the Views:

    <ListView ItemsSource={Binding OtherCollection} SelectedItem={Binding SomeCommand}>

This approach seems Okay to me, but this is my first contact with MVVM, and I wonder if this is how you do stuff.

I use FreshMvvm as the backing framework, and it uses conventions for bindings, so the view is automatically bound to its namesake partner.

Here is my current View, referencing the problem:

enter image description here

In this View you can see triple nesting (Carousel page -> Parent ListView -> Child ListView)

Now, if I have to bind a Command to an element in the child ListView (like the SelectedItem in the example), the Command should be located in the Meal class for all this to work. And that's a model class that shouldn't have commands, it should be purely a model.

The goal is to have a Command living in a ViewModel, bound to the Child ListView.

How do I solve this? Am I on the right path or is something totally wrong with my approach? I am open to a completely different approaches and suggestions.

Thanks for your time,

Sign In or Register to comment.