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.

Alvin Santos
  • 87
  • 1
  • 7
  • What have you tried so far? Have you came across dependency injection and event aggregator? – tomab Aug 08 '15 at 08: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. – Maurits van Beusekom Aug 08 '15 at 08: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? – Alvin Santos Aug 08 '15 at 08: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. – Maurits van Beusekom Aug 08 '15 at 09:13
  • can I access the model of my MainWindowViewModel from my usercontrol? – Alvin Santos Aug 08 '15 at 09:27

1 Answers1

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.

Community
  • 1
  • 1
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
  • Thank you for a clear explanation. By the way is this the traditional approach? – Alvin Santos Aug 08 '15 at 10:04
  • 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. – Ramin Bateni Aug 08 '15 at 10:18
  • 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 – Alvin Santos Aug 08 '15 at 10:50
  • how about global instance? creating viewmodel resources in application level? – Alvin Santos Aug 08 '15 at 11:08
  • 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](http://stackoverflow.com/questions/13447940/how-to-create-user-define-new-event-for-user-control-in-wpf-one-small-example). – Ramin Bateni Aug 08 '15 at 11:09
  • No, i think the B way is last way and it is useful in another mods. It is not good idea to use static properties to approach simple scenarios like which you want. You should pass data (SelecterPerson object) between `Usercontrols` or `Windows` similar to what I've described in my previous comment. – Ramin Bateni Aug 08 '15 at 11:19
  • But I rendered the user control programmatically, like what I said the frame is bounded to the model of all usercontrol.. I created a model that has a list of all usercontrols object and its name.. then calling the method from model to change the current object(usercontrol) to change the current usercontrol displayed in the mainwindow.. – Alvin Santos Aug 08 '15 at 11:19
  • I don't know why you keep a list of usercontrol objects and their names in a model!!! I wrote a standard way to play with your user controls. if you have additional question please write a new question and insert your classes and their scenario completely there. – Ramin Bateni Aug 08 '15 at 11:26