C# – MVVM Light & WPF – Binding Multiple instances of a Window to a ViewModel


I am working on my first project in MVVM and I've chosen to use the MVVM Light Toolkit. I have a GameViewModel that handles business on the main screen of my game. I need to find out how to open a new window (AdventurerView) with an instance of Adventurer as a parameter when a command is executed, have it bound to AdventurerViewModel, and display and return data. Instances of this window will be opened and closed frequently. I have been stuck on this for a couple of days now and it's driving me crazy. I would like to learn how to do this in an MVVM-friendly way, preferably with the tools provided by MVVM Light or pure XAML.

I've tried using MVVM Light's ViewModelLocator but since AdventurerView is a window it won't work; it says "Can't put a Window in a Style", though the program still compiles and runs. Could there be something I could change to make that work? Or is there another way to bind them in XAML? Or another approach entirely? I would really love to be able to move on from this. I have also tried using MVVM Light's messenger to no avail (which still doesn't tackle the View/ViewModel issue).

I just need to be able to create a window that is bound to AdventurerViewModel and display/return the appropriate data.

AdventurerView.xaml is in its default state at the moment, but I feel that if I could bind the appropriate data that might help (DataContext).

AdventurerViewModel is pretty bare-bones as well

class AdventurerViewModel : ViewModelBase
    #region Members

    private Adventurer _adv;


    #region Properties

    public Adventurer Adv
        get { return _adv; }
        set { _adv = value; }


    #region Construction

    public AdventurerViewModel(Adventurer adv)
        this._adv = adv;


App.xaml with the non-working DataTemplate at the bottom:

<Application StartupUri="MainWindow.xaml"

    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

    <DataTemplate DataType="{x:Type vm:GameViewModel}">
        <views:GameView />
    <DataTemplate DataType="{x:Type vm:TitleViewModel}">
        <views:TitleView />
    <DataTemplate DataType="{x:Type vm:AdventurerViewModel}">
        <views:AdventurerView />


The command in GameViewModel that will hopefully make this all happen (the messagebox just confirms that the command is firing):

    private void ExecuteShowAdvCommand(Adventurer adv)

I don't really know what else to include.

Best Solution

Ok I put together a demo that should make this hopefully easier for you Download Link


  • 3 Windows in Total (MainWindow, ModalWindow, NonModalWindow)
  • MainWindow has a TextBox you can type whatever you want into.
  • 2 buttons on the top will open the Modal / NonModal Window accordingly
  • Each window when opened will display the message that was in MainWindow's TextBox in a TextBlock inside them.
  • In each window you can tick a CheckBox to update the value in result's textblock in MainWindow (For the Modal Window this will kick in when modal window is closed. For NonModal changes can be seen asap)

That's it for functionality,


  • Registering Multiple VM's with the SimpleIoC and using GetInstance(...) to request them out.
  • Messenger class usage with a custom message type OpenWindowMessage
  • Opening Modal / Non Modal Windows from a parent VM staying true to the MVVM principles
  • Passing data between windows(just shown in NonModal)

Important Note: - The method used in this example to set the non DP DialogResult from the modal window is not MVVM friendly cos it uses code-behind to set the DialogResult property on a Window.Closing event which should be avoided(If needing to be "testable"). My preferred approach is a bit long and is very well documented HERE(Mixture of question and answer). Hence why I ignored it for the sake of this sample.

Related Question