tags:

views:

48

answers:

3

I'm developing a networked WPF application with the MVVM pattern and it seems that it's running and connecting to servers in the designer.

I know about the IsInDesignMode property, but I'm not sure how to access it in a ViewModel.

+2  A: 

Put a design time data source in your XAML like this:

<UserControl x:Class="Company.Product.View.MyView"
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:vm="clr-namespace:Company.Product.ViewModel.Design"
xmlns:design_vm="clr-namespace:Company.Product.ViewModel.Design"
mc:Ignorable="d" Name="MyView">
<UserControl.Resources>
    <ObjectDataProvider ObjectType="{x:Type design_vm:MyViewModel}" x:Key="DesignTime_DataSource" d:IsDataSource="True"/>
</UserControl.Resources>
<Grid d:DataContext="{StaticResource DesignTime_DataSource}">
....
</Grid>
</UserControl>

Let your design time viewmodel inherit from the run time viewmodel, but mock up the data in the constructor. You may also have to do something to your run time view model so the design time viewmodel doesn't run the data access code.

Guge
Looks like Visual Studio crashes when there's an unhandled exception in the ViewModel when done this way.
Brian Ortiz
My experience is that the design-window crashes gracefully, not the whole studio.
Guge
+1  A: 

It all depends on how you set up the binding between the view and the view-model. If it's initiated by the view in the constructor (which seems likely given the symptoms), you can check IsInDesignMode from there. Otherwise you need to provide a very quick overview of your architecture (or framework if you use any).

Alex Paven
All the binding is done in XAML as in Josh Smith's article on MVVM.
Brian Ortiz
+1  A: 
DependencyObject dep = new DependencyObject();
if (DesignerProperties.GetIsInDesignMode(dep))
{
    ...
}
sun1991