måndag 10 september 2012

WPF: Set a View's ViewModel for Designer to work against

To set a ViewModel for the Designer to work against you can do the following:
To the view's XAML code simply add the namespace to the viewmodel
xmlns:vm="clr-namespace:EOL.ViewModels"
and the following
d:DataContext="{d:DesignInstance Type=vm:ViewModel, IsDesignTimeCreatable=true}"

In the case of project named 'XYZ' with a UserControl as the view in a folder called  'Views' and a viewmodel called 'ViewModel' in the folder 'ViewModels' the XAML would look something like this:

<UserControl x:Class="XYZ.Views.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vm="clr-namespace:XYZ.ViewModels"
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance Type=vm:ViewModel, IsDesignTimeCreatable=true}">

Note that the viewmodel must have a parameterless constructor for this to work...

Inga kommentarer:

Skicka en kommentar