My app is in WPF,VS 2010 PRISM and Unity. i want to access Views Dockpanel control in viewmodel when constructor initialization for Adorner. any help would be appreciated.
Asked
Active
Viewed 4,271 times
1
-
3Hi there, the way you access controls is through binding, and UI should not bleed into the ViewModel. So the way to make it happen is to create properties and bind them to the Adorner properties you are trying to. – Oakcool Feb 18 '14 at 06:56
-
I agree with @Oakcool. In `ViewModel` should not be a direct reference to the controls that are in `View`. Add your question with concrete data that you need to get at DockPanel control. – Anatoliy Nikolaev Feb 18 '14 at 07:00
-
1@Oakcool- are commandparameter would be good? – Feb 18 '14 at 07:05
-
@Lucky CommandParameter is used in commands, so basically when you have a button, you may bind a command to it, and you might use CommandParameters – Oakcool Feb 18 '14 at 08:24
2 Answers
3
Yup. The correct response is you DON'T. In MVVM, the VM should have no knowledge of Views at all. You should be binding the View to the View Model.
However, there are of course certain cases where this pattern/model breaks down. At this point you can consider using the MVPVM pattern *. The class that MAY access the View is called the Presenter.
*Seriously, I'm not even kidding on this one. This link is a Microsoft magazine link.

Aron
- 15,464
- 3
- 31
- 64
1
Simple way is that you can resolve IUnityContainer
of View and can access control like:
readonly IUnityContainer _container;
public CONSTRUCTOR(IUnityContainer container)
{
_container=container;
var resolved = _container.Resolve<IEmployeeView>();
// cast your resolved view as View.
var views = resolved as YOURVIEWNAME;
// and get control.
var controls = views.YOURDOCPANELNAME;
}

Metro Smurf
- 37,266
- 20
- 108
- 140

J R B
- 2,069
- 4
- 17
- 32