1

For example I have a mainwindow that has a frame on it which binds to a model of all user controls that I have.

Example: (Basic Personal Information)

I have a usercontrol or (view-1) that display single person from my personalInformation database.

and I also have a usercontrol or (view-2) that display all the list of person in my database.

the question is how can I get the ID of single person from (view-2) to (view-1) as they are different usercontrol and different viewmodel as well.

what is the best approach to this kind of scenario? TIA.

5
  • What have you tried so far? Have you came across dependency injection and event aggregator? Commented Aug 8, 2015 at 8:35
  • Do you really need to have two different ViewModel classes? From the information in your question I would also see it fit to have one ViewModel containing a property to have a list of persons and a property for the selected person. Both Views could use the same ViewModel instance. Commented Aug 8, 2015 at 8:37
  • Just to have a clean separation of viewmodel for each views. I'm just thinking that having a single viewmodel for each views would be a great way when it comes to source code management, easy to read and maintain. just a thought. what do you think? Commented Aug 8, 2015 at 8:58
  • You are correct if you have commands that are specific for the one or the other view and are handled in the ViewModel. In that case maybe delegate the responsibility of keeping track of the selected person to the model. And inject the same model into the ViewModel classes. This could be done from the ViewModel which is serving the MainWindow. Commented Aug 8, 2015 at 9:13
  • can I access the model of my MainWindowViewModel from my usercontrol? Commented Aug 8, 2015 at 9:27

1 Answer 1

3

A

The Usercontrols are some elements to be part of your Window, then the Window can be the suitable connector between your UserControls. you can approach this scenario like this:

  1. WindowA
  2. UserControlPersonList
    • Include SelectedPersion Dependency Property. SelectedPerson type is a model class or viewmodel class
  3. UserControlPerson

Now, inside WindowA.xaml:

<StackPanel>
    <userControls:UserControlPersonList x:Name="PersonListControl"/>

    <userControls:UserControlPerson DataContext="{Binding ElementName=PersonListControl, Path=SelectedPerson}"/>
</StackPanel>

The result can be something like this (a master-detail view):

enter image description here

You need fill SelectedPerson dependency property of UserControlPersonList when you select a person. To perform this you can use Command and change SelectedPerson property in PersonListViewModel and bind SelectedPerson dependency property in UserControlPersonList to it OR do this in your UserControlPersonList level like this answer.


B

But if you want some global changed in your UserControls in different Windows you can hold the PersonListViewModel in a static property that is accessible in all of your Windows and Usercontrols of your program then create an event in it named SelectedPersionChanged. Now in your UserControls you can subscribe an EventHandler to SelectedPersionChanged and change your DataContext. But you MUST unsubscribe your EventHandler from SelectedPersionChanged when you did not need to that UserControl anymore to prevent memory leaks.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for a clear explanation. By the way is this the traditional approach?
First way (A) is a good, easy and safe approach. your codes are minimal and if you want you can hold the selected person in your ViewModel... sometimes its useful. The second way (B) is for a specific mode and i recommend you to use first way if your application need not real-time changes in some user controls in different windows.
Thanks sir RAM.. but how can I manage the navigation of controls? I mean how can I tell to the mainwindow to display the list or to display the selected person? TIA
how about global instance? creating viewmodel resources in application level?
This can do in different ways, you can create a master-detail view like the image i added to the answer. Or you can create a PersionDetail Window and try open it by passing the SelectedPersion info of PersionList usercontrol to its constractor. To impelement this scenario you need add an event in your PersonList usercontrol named SelectedPersonChanged like this answer.
|

Your Answer

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.