1

Does visual studio 2012 support conditional symbols for design time?

Example of my problem: In a WPF application with MVVM pattern, I create an instance of the ViewModel directly:

_viewModel = new OrdersViewModel();

but I want to use a conditional symbol for the design time only like this:

_viewModel = new OrdersViewModel
{
    Orders = new ObservableCollection<OrderModel>()
        {
            new OrderModel(){OrderId = "0e2fa124"},
            new OrderModel(){OrderId = "5wqsdgew"},
        }
};

For sure the conditional compilation symbols doesn't work.

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Mahmoud Samy
  • 2,822
  • 7
  • 34
  • 78
  • Possible duplicate: http://stackoverflow.com/questions/834283/is-there-a-way-to-check-if-wpf-is-currently-executing-in-design-mode-or-not – Denys Denysenko Aug 01 '13 at 12:33

2 Answers2

3

You should use design time DataContext in your view.

Something like this:

<Window x:Class="TestForDesignTimeData.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:MyNamespace="clr-namespace:Myproject.MyNamespace"
        mc:Ignorable="d" 
        Title="MainWindow" >
    <StackPanel d:DataContext="{d:DesignInstance MyNamespace:OrdersViewModel}"/>

Also keep in mind that you should derive OrderViewModel class and create a class with constructor to fill in those properties. Thus you will use one class for design time and the similar for real world. d: is a DesignTime interface

VidasV
  • 4,335
  • 1
  • 28
  • 50
2

You could use DesignerProperties.GetIsInDesignMode method.

_viewModel = new OrdersViewModel();
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
    _viewModel.Orders = new ObservableCollection<OrderModel>()
    {
        new OrderModel(){OrderId = "0e2fa124"},
        new OrderModel(){OrderId = "5wqsdgew"},
    };
};
Denys Denysenko
  • 7,598
  • 1
  • 20
  • 30