d:DesignInstance with an interface type d:DesignInstance with an interface type wpf wpf

d:DesignInstance with an interface type


I've found out how it's possible to overcome this illogical Visual Studio XAML designer error.

Instead of

<SomeControl d:DataContext={d:DesignInstance commons:IUser}>    <!--element content here--></SomeControl>

You can write

<SomeControl>    <d:SomeControl.DataContext>        <x:Type Type="commons:IUser" />    </d:SomeControl.DataContext>    <!--element content here--></SomeControl>

Yes, this solution doesn't look as cool but definitely it isn't much worse.

Type tag allows specifying interfaces, nested types (e.g. n:A+B) and even generics!

In case of generics just add a backtick and number of type arguments to your type: <x:Type Type="commons:User`1" />.

BTW all of these also works with styles, <d:Style.DataContext> doesn't produce errors!


You can take advantage of IsDesignTimeCreatable in this case like so:

d:DataContext="{d:DesignInstance commons:User, IsDesignTimeCreatable=False}"

which essentially instructs the designer to only use the type for intellisense and error highlighting instead of actually trying to instantiate a design instance.


I have just investigated more or less the same question... Actually, what I did is to follow the principles of MvvMLight. Precisely, I used a ViewModelLocator (which will be more or less static) so that the "right" ViewModel is injected at runtime or at design time. The magic lies in the function ViewModelBase.IsInDesignModeStatic provided by the MvvMLight framework. Finally, my ViewModelLocator class looks like

public class ViewModelLocator    {        private static readonly IKernel _kernel;        static ViewModelLocator()        {            _kernel = new StandardKernel();            if (ViewModelBase.IsInDesignModeStatic)            {                 _kernel.Bind<IBasicVM>().To<DesignBasicVm>();            }            else            {                _kernel.Bind<IBasicVM>().To<BasicVm>();            }        }        public IBasicVM BasicVm { get { return _kernel.Get<IBasicVM>(); } }    }

You may ignore the Ninject _kernel but you may need it (or a similar Ioc) if you are building your ViewModels with an IoC.

The App.xaml declares the ViewModelLocator as a ressource

  <Application.Resources>    <ResourceDictionary>      <viewModel:ViewModelLocator x:Key="ViewModelLocator" />    </ResourceDictionary>  </Application.Resources>

The MainWindow.DataContext property is bound to the BasicVM member of the ViewModelLocator. The Text property is bound the the GetContent member of the interface IBasicVM which is recognized statically by R# (at least R# 7.1 with VS2012)

<Window x:Class="WpfApplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        DataContext="{Binding BasicVm, Source={StaticResource ViewModelLocator}}"        >    <Grid>        <TextBlock  Text="{Binding GetContent}"/>    </Grid></Window>

You may check this repository which I have created as a template.