3

Our team is developing an application using WPF with MVVM.

We want to make ViewModels reusable. With this intention we want to abstract the transition logic between Views (ViewModels, we use ViewModel-first approach with Caliburn.Micro) so the other application can override it.

For instance, we have App1 and App2. Firstly, we developed App1 and it has FooViewModel which creates FunViewModel when user clicks a Next button.

But, developing App2 we want to get to NotFunViewModel instead of FunViewModel when user makes a click on a Next button.

How to accomplish such a task? The solution has to be universal and safe in accordance to types (in compile-time).

4

I usually use a ParentViewModel to control the application flow

The simplified version of it typically looks something along these lines:

App1ViewModel
{
    BaseViewModel CurrentView { get; set; }
    ICommand NextCommand { get; } <!-- will set CurrentView to FunViewModel -->
}

App2ViewModel
{
    BaseViewModel CurrentView { get; set; }
    ICommand NextCommand { get; } <!-- will set CurrentView to NotFunViewModel -->
}
<ContentControl Content="{Binding CurrentView}" />
<Button Command="{Binding NextCommand}" />
2

If you share a ViewModel in two Views and it shall behave differently, it certainly has some kind of switchable 'operation mode'.

You could just introduce a bool/enum property in your ViewModel and let the individual Views set its value to Mode1 or Mode2 respectively (in MVVM the View knows its ViewModel, but not vice versa => value could be set e.g. in View XAML).

Then the button click event or associated command can evaluate this mode-Property to choose the right action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.