1

Can someone please help me work out how to open a Dialog Window,

the simplest scenario I can think of is: We have a main window with a button and a Label,

when the user presses that button,

a dialog window with a text box and 2 buttons appear,

one button says submit,

when the user presses submit it closes the window,

it changes the color of the mainwindows background to red,

and takes the input placed in the textbox and changes a label on the main window to that content(I am bot so much worried about this part I get how to do this part),

while the other button just cancels the operation,

Assume that the Datacontext of the MainWindow and DialogWindow is MainWindowViewModel and UserInputViewModel respectivily.

Now on this link Cameron talks about using a service, ie IDialogService and DialogService now could someone please explain to me how to implement those methods in the scenario above? Or if there is another way to do this then please let me know? Please don't link me to any to any pages because I've probably read them all and I can't seem to get a clear understanding of what is meant to be happening? ~Slowly loosing his sanity because MVVM makes things so much harder :(

Community
  • 1
  • 1
Heinrich
  • 2,144
  • 3
  • 23
  • 39
  • Are you using any framework? Like Prism, MVVMLight or WAF etc? – Akash Kava May 13 '11 at 18:02
  • You might be interested in the **EmailClient** (ViewModel) sample application of the **[WPF Application Framework (WAF)](http://waf.codeplex.com)**. It shows how to show modal dialogs in a MVVM application. – jbe May 13 '11 at 17:27

2 Answers2

1

Not really an answer, but I think I'll add my POV anyway. How to use dialogs in an MVVM way, is something no one has really managed to do in an elegant fashion yet. There's basically 3 camps:
(1) The people who use an dialogservice like you described,
(2) The people who thinks that MVVM is good, but not something you should spent countless of hours trying to get right, so they use the codebehind, and
(3) people like me, who thinks that more often than not, the dialog and the parentview are so tied together that they should share viewmodels (as-in, the dialog is just one way of showing the data from your viewmodel).

cwap
  • 11,087
  • 8
  • 47
  • 61
  • Meant to post this as a comment, but the character limit hit me :( – cwap May 10 '11 at 22:29
  • 1
    Sharing same viewmodel is not good idea as you will end up addin more commands in view model that binds to different views leadin to piles of junk code. – Akash Kava May 13 '11 at 17:57
1

The learning curve for MVVM can be slightly steeper when attempting to do something a little more advanced than simple data binding. Have you checked out the MVVM Light toolkit? It includes a Messenger class that facilitates sending messages around the place. A listener registers for messages they want, and a sender just publishes them. In this manner, neither the listener or sender know about each other, but can communicate. Meaning the View can register for a message, and the ViewModel can send one.

This question talks about doing something very similar to what you want to do. I recommend the MVVM Light toolkit by the way!

I'm not sure about how you use the results of a dialog to send them through to the ViewModel. I'm assuming the harder part for you is communicating from the VM to the View.

Community
  • 1
  • 1
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164