laptop

Adding MVVM Light with NuGet

What is NuGet?

NuGet is a package manager that allows you to add packages (such as assemblies and source code) to an existing project. All versions of MVVM Light are supported through NuGet. To learn more and install NuGet, visit nuget.org.

Finding and Installing MVVM Light with NuGet

To add MVVM Light to your project using NuGet, follow these steps:

  1. Open your project in Visual Studio.
  2. Right-click on the References folder and select Manage NuGet Packages… from the context menu.Note: If this option is missing, you need to install the NuGet package manager from nuget.org.
  3. In the Manage NuGet Packages dialog, select Online on the right.
  4. Enter “mvvmlight” in the search field.
  5. Select the MVVM Light package and click the Install button (choose the top option for the full package).

Changes to Your Project

After installation, the following changes will be made to your project:

  • A packages folder will be added containing the necessary libraries. This folder is added to the root of the project structure but does not appear in the Solution Explorer.
  • A ViewModel folder will be added with two classes:
    • ViewModelLocator.cs
    • MainViewModel.cs
  • The App.xaml file will be modified to include the ViewModelLocator as a global resource.

Binding the DataContext in XAML

With the new file structure, the ViewModelLocator is exposed in App.xaml (global resources) and can be used as the source of the DataContext bindings. The MainViewModel added to the project is exposed in the ViewModelLocator as a property. For more details on this architecture, refer to Understanding the MVVM Pattern and Deep Dive MVVM.

Note: If you don’t need MainViewModel in your project, you can remove this class.

To use MainViewModel as the DataContext of the MainPage or MainWindow, follow these steps:

  1. Open MainPage.xaml or MainWindow.xaml.
  2. Add the following line to the opening tag of MainPage/MainWindow:xml

Binding the DataContext in Blend

You can also bind the DataContext visually in Blend:

  1. Open your project in Blend.
  2. Build the application.
  3. Open MainPage/MainWindow.
  4. In the Objects and Timeline panel, select the top element (Window, Page, etc.).
  5. In the Properties panel, select the DataContext property (in the Common Properties section).
  6. Click the small advanced options peg next to the property field and select Data Binding from the context menu.
  7. In the Data Binding editor, select the Data Field tab and the Locator data source. Note: If you don’t see the Locator field, you need to build the project.
  8. In the Fields area, expand ViewModelLocator and select the Main property. Click OK to close the Data Binding editor.

Installing Without the ViewModel Folder (Libraries Only)

If you’re adding MVVM Light to an existing application with an existing ViewModelLocator and/or viewmodels, you should install the “libraries only” NuGet package. Follow the steps above, but select MVVM Light libraries only instead of the full MVVM Light package.

Installing Portable Class Libraries

To install the portable class libraries (PCL) version of MVVM Light, follow the steps above and select MVVM Light libraries only (PCL).