Why viewmodel class doesn't has a reference to the view?

Why viewmodel class doesn't has a reference to the view?

Old forum URL: forums.lhotka.net/forums/t/8142.aspx


zhangxijun posted on Monday, December 14, 2009

Why viewmodel class doesn't has a reference to the view? In some cases, I need to implement some UI logic in viewmodel class, if it has a reference to the view, then I can manipulate the view directly, but now I have to find another way to do it.

Thanks

triplea replied on Monday, December 14, 2009

Well if there was a reference to the view then the View and ViewModel would be coupled tightly and it would be no different than having a code behind class.

What kind of UI logic would you implement in the ViewModel? I think you are thinking more with a WinForms frame of mind (nothing wrong there, I also get that and it is difficult to get rid of).

zhangxijun replied on Tuesday, December 15, 2009

Hi, triplea

Thanks your replay! For example, I need to implement a TCP/IP settings page(like windows TCP/IP property dialog), when use static IP settings, the radio button of automatic retrieve DNS server should be disabled, but when user click radio button of automaticly retrieve IP address, then the radio button of automatic retrieve DNS server should be enabled. I think this should belong UI logics. Where should I implement this logic? In codebehind or in viewmodel?

If the viewmodel should not have a reference to the view instance, then I think it is reasonable that the viewmodel has a reference to the interface of view.

If I want to implement it in viewmodel, then is there a easy to get the view behind the viewmodel?

Maybe CSLA's focus is on the bussesiss logic layer...

triplea replied on Tuesday, December 15, 2009

zhangxijun:

Maybe CSLA's focus is on the bussesiss logic layer...

Yes most definately. The ViewModel classes are just UI helper classes. CSLA is not a UI framework.

The thing with enabling/disabling elements is that you are thinking in terms of what you would do in WinForms. There you would capture the click event of the radio button and reapply authorization logic on your controls (just one way of doing it of course). I am no WPF expert but you would bind the Enabled properties so that when you make changes on your model, in XAML you would handle UI logic. There are a lot of ways of doing this so I would not attempt to expand. You probably need to play around with this stuff a bit more.

shawndewet replied on Tuesday, December 15, 2009

In agreement with triplea, you would have to expose appropriate "*Enabled" properties on your viewmodel, and then bind the IsEnabled property of the xaml controls to these properties on the viewmodel.

RockfordLhotka replied on Tuesday, December 15, 2009

If you want a full UI framework, there are several MVVM frameworks out there. CSLA is not a UI framework, it just has the minimum set of functionality to build a decent UI.

But you will find that the MVVM pattern itself says that the viewmodel doesn't reference the view. Such a reference would violate the pattern - so no reputable framework will support that concept, because it is not allowed by the pattern.

The idea is for the viewmodel to expose properties and methods, and to have the view bind to those properties and methods.

If the viewmodel has data that changes frequently, and the data should appear in the view, then the viewmodel should expose that data through properties and raise PropertyChanged when the data has changed.

Copyright (c) Marimer LLC