An easy way to get development-time data in Visual Studio 2010 is to use the constructor-datacontext. A short example with a window and ViewModel, for a DataContext, d: DataContext will be used in design mode, and StaticResource will be used at runtime. You can also use a separate ViewModel for design, but in this example I will use the same ViewModel for both.
<Window ... xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:DesignTimeData" mc:Ignorable="d" d:DataContext="{d:DesignInstance local:MyViewModel, IsDesignTimeCreatable=True}"> <Window.Resources> <local:MyViewModel x:Key="MyViewModel" /> </Window.Resources> <Window.DataContext> <StaticResource ResourceKey="MyViewModel"/> </Window.DataContext> <StackPanel> <TextBox Text="{Binding MyText}" Width="75" Height="25" Margin="6"/> </StackPanel> </Window>
And in the ViewModels MyText property, we check to see if we are in development mode, in which case we will return something else.
public class MyViewModel : INotifyPropertyChanged { public MyViewModel() { MyText = "Runtime-Text"; } private string m_myText; public string MyText { get {
Designer.cs, which is located here , is as follows
public static class Designer { private static readonly bool isDesignMode; public static bool IsDesignMode { get { return isDesignMode; } } static Designer() { DependencyProperty prop = DesignerProperties.IsInDesignModeProperty; isDesignMode = (bool)DependencyPropertyDescriptor. FromProperty(prop, typeof(FrameworkElement)) .Metadata.DefaultValue; } }
Fredrik hedblad
source share