Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DesignInstance does not work in VS2013

Visual Studio does not show design time data with DesignInstance attribute. I have checked DesignInstance with/without MVVM Light. I have spend a lot of time to fix the issue (checked similar queestions on StackOverflow too) but DesignInstance simply does not work.

Project:

  • SearchIdView.
  • SearchIdViewModel - real View Model.
  • DesignSearchIdViewModel - inherits from SearchIdViewModel and contains design time data (properties are assigned in constructor).

Environment:

  • VS2013 SP3
  • Net 4.0
  • MvvmLight 5.0.2.0

SearchIdView.xaml

<Window x:Class="App1.View.SearchIdView"
    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:ignore="http://www.ignore.com"
    xmlns:design="clr-namespace:App1.Design"
    mc:Ignorable="d ignore"
    DataContext="{Binding SearchId, Source={StaticResource Locator}}"
    d:DataContext="{d:DesignInstance d:Type=design:DesignSearchIdViewModel,IsDesignTimeCreatable=True}"
    >
<Grid>
    <TextBlock Text="{Binding Test}" />
</Grid>

SearchIdViewModel.cs

Property from SearchIdViewModel

public const string TestPropertyName = "Test";
private string _test;
public string Test
{
  get
  {
    return _test;
  }
  set
  {
    Set(TestPropertyName, ref _test, value);
  }
}

Do you have any idea why DesignInstance does not work in this case?

Workaround

  • remove d:DataContext from view
  • add interface ISearchIdViewModel (it is empty)
  • SearchIdViewModel inherits from ISearchIdViewModel
  • change ViewModelLocator (below)

ViewModelLocator.cs

public class ViewModelLocator
{
  static ViewModelLocator()
  {
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    if (ViewModelBase.IsInDesignModeStatic)
    {
      SimpleIoc.Default.Register<ISearchIdViewModel,Design.DesignSearchIdViewModel>();
    }
    else
    {
    SimpleIoc.Default.Register<ISearchIdViewModel, SearchIdViewModel>();
    }
  }

  public SearchIdViewModel SearchId
  {
    get { return (SearchIdViewModel) ServiceLocator.Current.GetInstance<ISearchIdViewModel>(); }
  }
}
like image 639
Serge P Avatar asked Dec 19 '22 09:12

Serge P


2 Answers

Your d:DesignInstance declaration is malformed. You specify the property name d:Type instead of Type, so the property is not assigned correctly. Either replace d:Type with Type, or leave the property name off entirely and let it be inferred as the default property.

d:DataContext="{d:DesignInstance d:Type=design:DesignSearchIdViewModel,
                                 IsDesignTimeCreatable=True}"

Should become:

d:DataContext="{d:DesignInstance Type=design:DesignSearchIdViewModel,
                                 IsDesignTimeCreatable=True}"

Or, alternatively:

d:DataContext="{d:DesignInstance design:DesignSearchIdViewModel,
                                 IsDesignTimeCreatable=True}"

(line wrapping added for readability)

like image 193
Roni Hoffman Avatar answered Dec 21 '22 23:12

Roni Hoffman


Another cause that might make d:DesignInstance not to work is that all data must be properties not just public variables of mock class! I know that it was not your problem, but it should be checked if for someone it does not work.

Will not work with:

public class MockFile
{
    public FilePRJO FilePRJO = new FilePRJO();
}

But it will work with:

public class MockFile
{
    public FilePRJO _filePRJO = new FilePRJO();

    public FilePRJO FilePRJO
    {
        get
        {
            return _filePRJO;
        }
    }
}
like image 40
Evalds Urtans Avatar answered Dec 21 '22 23:12

Evalds Urtans