This was previously posted in StackOverflow, I just moved to here...

I am using VS2008, MVVM, WPF, Prism to make a mutiple window CRM Application.

I am using MidWinow in my MainWindow, I want

  1. Any ViewModel would able to make request to MainWindow to create/add/close MidChildWindow, ChildWindow(from WPF Toolkit), Window (the Window type).
  2. ViewModel can get the DialogResult from the ChildWindow its excutes.
  3. MainWindow have control on all opened window types.

Here is my current approach:

I made Dictionary of each of the windows type and stores them into MainWindow class.

For 1, i.e in a CustomerInformationView, its CustomerInformationViewModel can execute EditCommand and use EventAggregator to tell MainWindow to open a new ChildWindow.


CustomerEditView ceView = new CustomerEditView ();
CustomerEditViewModel ceViewModel = CustomerEditViewModel ();
ceView.DataContext = ceViewModel;
ChildWindow cWindow = new ChildWindow();
cWindow.Content = ceView;
MainWindow.EvntAggregator.GetEvent<NewWindowEvent>().Publish(new WindowEventArgs(ceViewModel.ViewModeGUID, cWindow ));

Notice that all my ViewModel will generates a Guid for help identifies the ChildWindow from MainWindow's dictionary. Since I will only be using 1 View 1 ViewModel for every Window.

For 2. In CustomerInformationViewModel I can get DialogResult by OnClosing event from ChildWindow, in CustomerEditViewModel can use Guid to tell MainWindow to close the ChildWindow.

Here is little question and problems:

Is it good idea to use Guid here? Or should I use HashKey from ChildWindow?

My MainWindows contains windows reference collections. So whenever window close, it will get notifies to remove from the collection by OnClosing event. But all the Windows itself doesn't know about its associated Guid, so when I remove it, I have to search for every KeyValuePair to compares...

I still kind of feel wrong associate ViewModel's Guid for ChildWindow, it would make more sense if ChildWindow has it own ID then ViewModel associate with it...

But most important, is there any better approach on this design? How can I improve this better?

  • 1
    Can you post the code from MainWindow? Does it expose any public properties? Apr 2, 2012 at 14:41

1 Answer 1


Instead of trying to use a Guid or HashKey, you might find it easier to actually use a reference to the ChildWindow objects.

Also, in order to prevent memory leaks, while not having to worry about handling events, you can use a weak reference to the object so that the garbage collector can still reclaim the resources used by the object once it's no longer referenced anywhere.

I've found the folliwing .NET classes to be pretty handy when working with weak references:

Also, you can have each ChildWindow set it's own DataContext to the appropriate ViewModel, either declaratively within the XAML, or in the code-behind.


Also, I thought I should clarify that if you use a collection of WeakReference's to keep track of each window, you won't need to manually remove the reference from the collection whenever a window is closed. You can simply close your window (either from the code-behind, or anywhere else) & forget about it - the GC will still automatically deallocate the necessary resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.