silverfighter
silverfighter

Reputation: 6882

What makes visual studio's designer kick in for design time support

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.

Upvotes: 5

Views: 890

Answers (2)

Ziriax
Ziriax

Reputation: 1040

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>

Upvotes: 0

GazTheDestroyer
GazTheDestroyer

Reputation: 21261

Try

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

There is a blog here that may help you too.

Upvotes: 5

Related Questions