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.
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.
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.
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).
DependencyObject dep = new DependencyObject();
if (DesignerProperties.GetIsInDesignMode(dep))
{
...
}