5

I have a c# control library which contains my models, viewmodels and views. I hook everything up as I usually do but I do not get any design time feedback from visual studio's designer (blendability).

When I load my assambly in a WPF project and include the view as custom user control I'll get my design time feedback. Unfortunately this WPF Project is only a test shell because the view will live in another app.

It would be more efficient for my dev pipeline if I could have blendability (design time) support in my class library? What makes visual studio kick in to show my design time datacontext?

I even use d:DataContext="{d:DesignInstance dd:DesignViewModel}" in my class library. No design time data in class library.

silverfighter
  • 6,762
  • 10
  • 46
  • 73
  • Can you manaully open your views up in the editor? Right-click, open with "Windows Presentation Designer"? I ask because I can just double-click a XAML file in a class library and I get the designer so I'm not sure why you can't. – Michael Edenfield Mar 26 '12 at 16:30
  • Yeah the xaml file is loaded but it is not published with design time data (DataContext hooked up with mockdata). Like it is when I open it with a Xaml Application – silverfighter Mar 26 '12 at 18:17
  • Are you using a specific mvvm framework? – Derek Beattie Mar 26 '12 at 21:24
  • @derek I use MVVMlight but I do not see it related to any framework because at that point I do basic stuff which is not framework related – silverfighter Mar 27 '12 at 07:42
  • If it was Caliburn Micro, for example, you'd need to set Bind.AtDesignTime="True". For MvvmLight, it's different. – Derek Beattie Mar 27 '12 at 15:33

2 Answers2

5

Try

d:DataContext="{d:DesignInstance dd:DesignViewModel, IsDesignTimeCreatable=True}

There is a blog here that may help you too.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0

I found it really frustrating having to create either an empty constructor for my viewmodels, or to make derived classes all the time, just to please the WPF designer.

One solution that works for me (tested only with Visual Studio 2013) is using a static property to expose an instance of a design-time view model, for example

C# code

namespace WpfApplication2
{
    public class Person
    {
        public Person(string id)
        {
            Id = id;
        }

        public string Id { get; private set; }
    }

    public static class DesignViewModels
    {
        public static Person Person
        {
            get { return new Person("Design time person id"); }
        }
    }
}

and the XAML

<Window x:Class="WpfApplication2.MainWindow"
                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:my="clr-namespace:WpfApplication2"
                mc:Ignorable="d"
                Title="MainWindow" Height="350" Width="525">
    <d:DesignProperties.DataContext>
        <x:Static Member="my:DesignViewModels.Person" />
    </d:DesignProperties.DataContext>
    <TextBlock Text="{Binding Id}"/>
</Window>
Ziriax
  • 1,012
  • 10
  • 19