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).

asked Dec 18, 2013 at 8:56
0

2 Answers 2

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}" />
answered Dec 20, 2013 at 15:21
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.

gnat
20.5k29 gold badges117 silver badges308 bronze badges
answered Dec 20, 2013 at 9:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.