Share via


Design-Time Attributes in the Silverlight Designer

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When you build Silverlight applications by using the Silverlight Designer for Visual Studio 2010, you sometimes need to provide information to make Design view behave correctly. You specify this kind of information by using design-time attributes. For example, design-time attributes enable sizing the root page with specific values to accommodate layout design, while retaining content-driven sizing at run time. Design-time attributes are ignored during compilation and have no effect at run time.

Design-time Attributes

The Silverlight Designer provides the following design-time attributes.

Design-time Attribute

Description

Example Usage

d:DesignHeight and d:DesignWidth

Specifies the height and width of the root element at design time, independently of the height at run time.

<UserControl x:Class="DesignDataDemo.MainPage"
  ...
  mc:Ignorable="d"
  d:DesignHeight="300" d:DesignWidth="400" >

d:DataContext

Specifies a design-time data context for a control and its children. A common pattern is to declare control bindings in XAML view, and to set the DataContext that is used to populate the bindings at run time. If you are using this pattern, you can set d:DataContext so that the designer is aware of the shape of your types. This enables you to use the data binding builder to create bindings in Design view. For more information, see Walkthrough: Creating a Data Binding by Using the Silverlight Designer.

<Grid x:Name="LayoutRoot" Background="White"
  d:DataContext="{d:DesignInstance
  Type=local:Customer}">

d:DesignInstance

Used as part of a d:DataContext or d:DesignSource declaration. Specifies the type that you can use as a data source for binding to controls in the designer. The type does not need to be creatable in XAML. For more information, see Walkthrough: Using a DesignInstance to Bind to Data in the Silverlight Designer.

<Grid x:Name="LayoutRoot" Background="White"
  d:DataContext="{d:DesignInstance
  Type=local:Customer}">

d:DesignData

Used as part of a d:DataContext or d:DesignSource declaration. Specifies a XAML file that contains sample data for use at design time. Use the DesignData or DesignDataWithDesignTimeCreatableTypes build actions to integrate your sample data with your project. Read-only properties can be assigned values. For more information, see Walkthrough: Using Sample Data in the Silverlight Designer.

<StackPanel
  d:DataContext="{d:DesignData
  Source=./DesignData/SampleCustomer.xaml}">

d:DesignSource

Specifies a design-time data source for a CollectionViewSource. This makes the designer aware of the shape of your types. This enables you to use the data binding builder to create bindings.

<CollectionViewSource x:Key="CustomerViewSource"
  d:DesignSource="{d:DesignInstance
  local:Customer, CreateList=True}" /> 

d:IsDesignTimeCreatable

In the d:DesignInstance markup extension, specifies that the design instance is created from your type, instead of a designer-generated substitute type.

<Grid d:DataContext="{d:DesignInstance
  local:Customer, IsDesignTimeCreatable=True}">

d:CreateList

In the d:DesignInstance markup extension, specifies that the design instance is a list of the specified type.

<CollectionViewSource x:Key="CustomerViewSource"
  d:DesignSource="{d:DesignInstance
  local:Customer, CreateList=True}" />

d:Type

In the d:DesignInstance markup extension, specifies the type to create. Use d:IsDesignTimeCreatable to specify whether an instance or your type or a designer-generated substitute type is created.

<CollectionViewSource x:Key="CustomerViewSource"
  d:DesignSource="{d:DesignInstance
  Type=local:Customer, CreateList=True}" />

Accessing Design-time Attributes

You access the design-time attributes through the https://schemas.microsoft.com/expression/blend/2008 namespace. By default, Visual Studio maps this namespace to the d: prefix.

Build Actions

To enable d:DesignData, you set build actions on the XAML files that contain your sample data. The following table describes the build actions. For more information, see Walkthrough: Using Sample Data in the Silverlight Designer.

Build Action

Description

DesignData

Use this build action when the sample data types are not creatable or have read-only properties that you want to define sample data values for. The Silverlight Designer creates substitute types that have the same properties as your business object types. Your types do not need to be creatable. This eliminates complexity around factory methods, abstract types, and database connections. Read-only properties can be assigned values.

DesignDataWithDesignTimeCreatableTypes

Use this build action when the sample data types are creatable by using their default empty constructors. The Silverlight Designer creates instances of your types that are defined in the sample data file. Your types must be creatable in XAML.