Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An easy way to get Design-time-data in Visual Studio 2010 is to use a design-datacontext. Short example with a Window and a ViewModel, For DataContext, the d:DataContext will be used in Design-mode and the StaticResource will be used in runtime. You can also use a separate ViewModel for design but in this example I will use the same ViewModel for both.</p> <pre><code>&lt;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}"&gt; &lt;Window.Resources&gt; &lt;local:MyViewModel x:Key="MyViewModel" /&gt; &lt;/Window.Resources&gt; &lt;Window.DataContext&gt; &lt;StaticResource ResourceKey="MyViewModel"/&gt; &lt;/Window.DataContext&gt; &lt;StackPanel&gt; &lt;TextBox Text="{Binding MyText}" Width="75" Height="25" Margin="6"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>And in the ViewModels property MyText we check if we're in design mode and in that case we return something else.</p> <pre><code>public class MyViewModel : INotifyPropertyChanged { public MyViewModel() { MyText = "Runtime-Text"; } private string m_myText; public string MyText { get { // Or you can use // DesignerProperties.GetIsInDesignMode(this) if (Designer.IsDesignMode) { return "Design-Text"; } return m_myText; } set { m_myText = value; OnPropertyChanged("MyText"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } </code></pre> <p>Designer.cs, which is found <a href="http://sachabarber.net/?p=707" rel="noreferrer">here</a>, looks like this</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload