2

My WPF app doesn't use an app.xaml. I'm using MVVM, and construct the view and the viewmodel separately. I then pass the ViewModel to the View constructor and set the datacontext there.

I'd like to have Visual Studio be able to understand the viewmodel's properties for context clicking and such, but not have it construct its own DataContext when the view is set.

When I do this, it forces me to have a default constructor for MainViewModel, and then it calls that VM constructor when I construct the View.

<Window x:Class="Kraken.CopFeed.Windows.MainFeedView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Windows="clr-namespace:MyProgram.MainApp.WpfWindows"
        mc:Ignorable="d"
        Title="Main App" Height="321.557" Width="652.922">

    <Window.DataContext>
        <Windows:MainViewModel />
    </Window.DataContext>

How can I keep my existing implementation of constructing the V and VM separately, but get the XAML editor in Visual STudio to know of my ViewModel that will (eventually) be set as the data context?

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176

1 Answers1

4

I think you should be able to do this with d:DesignInstance, as in this question.

<Window
    ...etc...
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"

    d:DataContext="{d:DesignInstance Type=MyViewModelNamespace:MyViewModel}"
    >
Community
  • 1
  • 1
  • is IsDesignTimeCreatable necessary? i didn't set it and it seems to work fine. I'm referencing a concrete ViewModel, not an interface. Can't find exact info on what IsDesignTimeCreatable does. – Stealth Rabbi Dec 12 '16 at 20:02
  • 1
    I just included that because it was easier to paste the whole thing intact than test it. I'll zap that if it's working fine for you without it. – 15ee8f99-57ff-4f92-890c-b56153 Dec 12 '16 at 20:06
  • 1
    `IsDesignTimeCreatable` is not necessary for intellisense, only if you want to see data in the graphical designer. – Petter Hesselberg Dec 14 '16 at 14:45