6

I have UserControl displaying file information. And I want to see test file information in visual studio designer using view-model mock. Here is XAML:

<Label Content="{Binding DateCreated}"/>

View-model:

internal class ViewModel
{
    public ViewModel(string pathToFile) 
    {
        var file = new FileInfo(pathToFile);
        this.DateCreated= file.CreationTime.ToShortDateString();
    }

    public string DateCreated{ get; set; }
}

In design-time I see real file information in designer using view-model mock class:

internal class ViewModelMock : ViewModel
{
    public ViewModelMock() : base(@"D:\Images\mock.png")
    {
    }
}

I plug mock into xaml via d:DataContext and d:DesignInstance:

<UserControl ...
    xmlns:mocks="clr-namespace:Company.Product.Mocks"
    d:DataContext="{d:DesignInstance mocks:ViewModelMock, DesignTimeCreatable=True}">

It works, but the problem is in this @"D:\Images\mock.png" hardcoded value. As soon as solution powered by TFS, I need to place the image into the project and reference this image from ViewModelMock, relative to project path.

How can I do that?

Simple methods like getting current working directory gives 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE', I guess because this is location of XDesProc.exe which responsible for visual studio designer.

Currently I'm using EnvDTE method described in this questions:

var solutionFullName = ((EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0")).Solution.FullName;
var projectPath = Path.Combine(Path.GetDirectoryName(solutionFullName), "ProjectFolderName");

But this has almost the same problem - hardcoded "VisualStudio.DTE.12.0" constant (which means Visual Studio 2013), so I'm still trying to find better solution.

Community
  • 1
  • 1
Sam
  • 1,384
  • 2
  • 20
  • 30
  • Same problem here in 2020, have you found the solution? – hybrid2102 Apr 30 '20 at 17:26
  • @hybrid2102 Nope, and I didn't research this problem since, so it would be interesting to know whether situation changed in VS2019, or better hack involved :) – Sam May 04 '20 at 14:31

0 Answers0