Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF/MVVM - should view-models stay constant and just the model change?

Tags:

mvvm

wpf

I'm a bit in doubt as to what is 'the right way'.

I have an application with concepts like visual studio, so I'll use that to explain:

I have a 'solution' view model and a model behind. The view model is displayed in a 'explorer'.

I can change between the situations where 'no solution is loaded' to 'a solution is loaded' and back.

And finally my question :-) :

Should I keep my view model object and let it reflect that I have a new 'solution object' loaded? Or should I create a new view model object and let the view bind to the new object?

like image 363
Anders Juul Avatar asked Jan 02 '26 08:01

Anders Juul


2 Answers

Your Viewmodel contains the state of any data associated with the UI which is not further back in the Model.

One way I answer questions such as yours is by considering what behaviour I want in the UI and what needs binding to some state information. Or, to put it a different way, any time that I feel like writing some code that would cause UI elements to be shown or hidden, think about how that maps to a boolean variable.

So, take large chunks of the UI that only are visible when you have a Model loaded. These might have their visibility bound to a boolean property in the Viewmodel IsSolutionLoaded.

Maybe you want to disable some things if processing is occurring, you could have a property IsCompiling. I've used this approach with a property NotRunningthreadedProcessing as shown below, that let me disable controls when a synchronisation object existed in the Viewmodel.

CNTL_WhiteLevel.SetBinding(ProgressBar.IsEnabledProperty, 
    new Binding("NotRunningThreadedProcessing"));

// and the C++/CLI property concerned
property bool NotRunningThreadedProcessing {
    bool get()
    {
        return mThreadedCommandSyncher == nullptr;
    }
};
like image 52
Andy Dent Avatar answered Jan 05 '26 04:01

Andy Dent


What is the arrengement of the ViewModel?

In General

View Model is the localized version of actualmodel and View is updated whenever there is a change i its viewModel through DataBiding.

in your case ,you have 2 states.

1- Your View is Loaded

2- Your View is not loaded

so should I create a new view model object and let the view bind to the new object?

in my thoughts ,YES

like image 22
TalentTuner Avatar answered Jan 05 '26 04:01

TalentTuner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!