0

I have implemented the navigation service of WPF to navigate from a view to another. I do this by using the ViewModelLocator that is responsible to navigate from a view to another each time it receives the proper "Galasoft Messenger".

All right.

Now, having a lot of Views-ViewModels couples, I decided to create an user control that is shared (included in XAML) in some views. This UserControl has its own ViewModel and some property to set its appearence depending by the View is currently shown.

How I can access the property of the ViewModel of the UserControl from the View currently shown (that has inside the user control)?

And more, how (and where) I can determine the view currently shown?

I can post some code to help if needed, but I guess the answer is short and simple but I somehow missed something...

Thank you.

L

zero51
  • 801
  • 2
  • 8
  • 20

1 Answers1

0

When I host user controls where I would like access to the View Model I typically use a ContentControl hooked into a databound DataTemplate like in this answer. If you needed to switch views that the View Model uses you could use a trigger like in this answer.

With that setup you would be able to set all the properties you need the user control's view model as well as know which view the user control is using.

Community
  • 1
  • 1
Erick
  • 486
  • 2
  • 12
  • Ok, but the user control in this way doesn't need its own viewmodel, am I right? In your scenario you could have several views for the same viewModel... – zero51 Apr 13 '12 at 06:55
  • This setup would allow for several views to have the same view model. If you have several pages each page typically would have it's own view model and the user control user control would have a seperate view model so that the functionality of the user control could be seperate from the pages that host it. – Erick Apr 13 '12 at 13:28