0

I am very new to WPF and am struggling with the most basic of tasks. I really hope someone can point me in the right direction please. I am using WPF with Modern UI utilising the MVVM pattern.

I have a UserControl which is successfully displaying some data in a datagrid. All I want to do is, via a button click, navigate to a second page (Usercontrol) and show some details of the selected row for editing etc.

I have found several examples of master detail setups on the same page but none that answers my questions using different windows/pages/usercontrols.

This seems like it should be the most simple of tasks but for some reason I am really struggling. I hope I am just missing something obvious as I am feeling pretty stupid about now.

Thanks in advance.

MattR
  • 13
  • 4
  • Have your second UserControl expose a dependency property and bind it to the selected item of the data grid. That's the MVVM way. No messaging required –  Feb 14 '15 at 16:46
  • Aren't dependency properties technically the WPF way? In the question, there is a suggestion that these controls are running in different page contexts so the child control does not exist in a way that is bindable from the parent. If they do coexist, then absolutely this is better than messaging. – kidshaw Feb 15 '15 at 08:29
  • Thanks for the comment, I need to understand dependency properties and messaging a lot more than I already do. Do you know any good resources that help when deciding which method to use when? – MattR Feb 16 '15 at 15:02

1 Answers1

0

To use different controls together, you need to make the view models communicate. This can be done by passing references, or more commonly via a messaging framework.

I prefer MVVM Light from NuGet. It allows each view model to send and register to messages of specific types.

It's kind of like broadcasting events but allows your parent view model to send messages to the child view model without actually knowing about it.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
  • Thanks for the suggestion, I am working through a tutorial on MVVMLight now. Hopefully that will help. – MattR Feb 16 '15 at 14:59
  • If it helps, I've answered a similar question here http://stackoverflow.com/questions/25158972/how-to-establish-communication-pass-data-between-two-viewmodels/25159514#25159514 – kidshaw Feb 16 '15 at 15:13
  • Thanks again @kidshaw. I think I am getting somewhere with the messaging now but still a little confused. I can now send a message from the parent and register to receive the message in the child but at the moment I am creating the child viewmodel when the view is created so it does not exists to register for the initial send. Should I be creating the child viewmodel at the same time as the parents? or am I going wrong with my thinking somewhere? – MattR Feb 17 '15 at 14:43
  • MVVM Light will save the day! You need your viewmodels to exist before the view so use the ViewModelLocator pattern it provides with SimpleIOC. Basically you create a class that exposes your view models as properties then in app.xaml create an instance of that class as a data source. Your views are then bound to the relevant property for the view model they need. Using SimpleIOC under the hood helps ensure only one singleton of your ViewModel exists. Best part, if you install the full package from nuget, this is all there by default. – kidshaw Feb 17 '15 at 20:05