Prism 5 and Unity - Manually resolve View with ViewModel wired up
<p><strong>Using:</strong> WPF, Prism 5, Unity</p>

<p>I am trying to write what I call a "WindowDialogService" which, when passed a type and called, will open a Window with that type as the content. My problem is I cannot get the ViewModel to instantiate and associate to the View.</p>

<p>I don't know if it is right but I am using <code>AutoWireViewModel</code> in my View and was assuming (obviously incorrectly) that the ViewModel would be "Located" when resolving using the Unity Container i.e. <code>container.TryResolve &lt;T&gt; ()</code></p>

<p>So basically, I can resolve the View but the DataContext of the view is null.</p>

<p>I have included all relevant code below.</p>

<p><strong>View</strong></p>

<pre><code>&lt;dxc:DXWindow x:Class="ListClassList.Views.AddToClassView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"      
              prism:ViewModelLocator.AutoWireViewModel="True"
              xmlns:dxe="clr-namespace:DevExpress.Xpf.Editors.Settings;assembly=DevExpress.Xpf.Core.v15.2"
              xmlns:dxeditors="http://schemas.devexpress.com/winfx/2008/xaml/editors"
              xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core" 
              xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
             xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"              
             xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
                    WindowStartupLocation="CenterScreen"
                    dxc:ThemeManager.ThemeName="VS2010"
                    Title="{Binding Title}"
                    Width="800"
                    Height="500"
              ResizeMode="CanResizeWithGrip"
             &gt;
    &lt;Grid&gt;
    &lt;/Grid&gt;
&lt;/dxc:DXWindow&gt;
</code></pre>

<p><strong>ViewModel</strong></p>

<pre><code>using MyApp.Data;
using MyApp.Models.Model;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace ListClassList.ViewModels
{
    public class AddToClassViewViewModel : BindableBase
    {
        private readonly MyAppDbContext _context;

        private ObservableCollection&lt;MasterClass&gt; _masterClasses;
        public ObservableCollection&lt;MasterClass&gt; MasterClasses
        {
            get { return _masterClasses; }
            set { SetProperty(ref _masterClasses, value); }
        }

        private ObservableCollection&lt;Student&gt; _students;
        public ObservableCollection&lt;Student&gt; Students
        {
            get { return _students; }
            set
            {
                SetProperty(ref _students, value);
            }
        }


        public DelegateCommand FinishCommand { get; set; }


        public AddToClassViewViewModel(IStudentTimetableService studentTimetableService)
        {


        }

    }
}
</code></pre>

<p><strong>IWindowDialogService Interface</strong></p>

<pre><code>using System;
using System.Windows;
    namespace MyApp.Infrastructure.Services.Dialogs.WindowDialog
    {
        public interface IWindowDialogService
        {
            bool? ShowDialog&lt;T&gt;(Action onDialogClosed) where T : Window;
        }
    }
</code></pre>

<p><strong>WindowDialogService Implementation</strong></p>

<pre><code>using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Practices.ServiceLocation;
using Prism.Unity;


namespace MyApp.Infrastructure.Services.Dialogs.WindowDialog
{
    public sealed class WindowDialogService : IWindowDialogService
    {
        private readonly List&lt;Window&gt; _openWindows = new List&lt;Window&gt;();
        private static volatile WindowDialogService _instance;
        private static readonly object SyncRoot = new Object();

        private WindowDialogService() { }

        public static WindowDialogService Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (_instance == null)
                            _instance = new WindowDialogService();
                    }
                }
                return _instance;
            }
        }


        public bool? ShowDialog&lt;T&gt;(Action onDialogClosed) where T : Window
        {
            try
            {
                 using (var container = new Microsoft.Practices.Unity.UnityContainer())
                {
                    var dialog = (Window)container.TryResolve &lt;T&gt; ();
                    dialog.Closed += (s, e) =&gt; onDialogClosed();
                    var result = dialog.ShowDialog();
                    return result;
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
</code></pre>