Reputation: 10346
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?
Upvotes: 2
Views: 921
Reputation: 37059
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}"
>
Upvotes: 4