设计时特性

在使用适用于 Visual Studio 的 WPF 设计器生成 WPF 或 Silverlight 应用程序时,有时需要提供一些信息来使“设计”视图正常运行。 可以使用设计时特性指定这种信息。例如,使用设计时特性,可以用特定值调整根窗口的大小,使其适应布局设计,并同时在运行时保留内容驱动的大小。 设计时特性在编译过程中将被忽略,它们在运行时不会产生任何影响。

设计时特性

WPF 设计器提供下列设计时特性。

设计时特性

说明

示例用法

d:DesignHeight

在设计时指定根元素的高度,该高度与运行时的高度无关。 在您单击根大小标记 (根大小标记) 时自动增加。

<Window x:Class="DesignDataDemo.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DesignDataDemo"
        Title="MainWindow" mc:Ignorable="d" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="350" d:DesignWidth="525" SizeToContent="WidthAndHeight">

d:DesignWidth

在设计时指定根元素的宽度,该宽度与运行时的宽度无关。 在您单击根大小标记 (根大小标记) 时自动增加。

<Window x:Class="DesignDataDemo.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DesignDataDemo"
        Title="MainWindow" mc:Ignorable="d" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="350" d:DesignWidth="525" SizeToContent="WidthAndHeight">

d:DataContext

为控件及其子控件指定设计时数据上下文。 一个常见模式是:在 XAML 视图中声明控件绑定并设置用来在运行时填充绑定的 DataContext。 如果使用此模式,则可以设置 d:DataContext,以便设计器能够识别您所用类型的形状。 这使您能够使用数据绑定生成器,在“设计”视图中创建绑定。 有关更多信息,请参见演练:使用 WPF 设计器创建数据绑定

<Grid d:DataContext="{d:DesignInstance Type=local:Customer}" Name="_grid">

d:DesignInstance

用作 d:DataContext 或 d:DesignSource 声明的一部分。 指定要用作数据源而且要绑定到设计器中控件的类型。 该类型在 XAML 中不一定是可创建的。 有关更多信息,请参见演练:在设计器中使用 DesignInstance 绑定到数据

<Grid d:DataContext="{d:DesignInstance Type=local:Customer}" Name="_grid">

d:DesignData

用作 d:DataContext 或 d:DesignSource 声明的一部分。 指定一个 XAML 文件,其中包含要在设计时使用的示例数据。 使用“DesignData”“DesignDataWithDesignTimeCreatableTypes”生成操作可以将示例数据与您的项目集成。 可以为只读属性赋值。 有关更多信息,请参见演练:使用 WPF 设计器中的示例数据

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

d:DesignSource

CollectionViewSource 指定设计时数据源。 这使设计器能够识别您所用类型的形状。 这样便可使用数据绑定生成器创建绑定。

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

d:IsDesignTimeCreatable

在 d:DesignInstance 标记扩展中,指定根据您的类型创建设计实例,而非设计器生成的替代类型。

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

d:CreateList

在 d:DesignInstance 标记扩展中,指定设计实例是指定类型的列表。

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

d:Type

在 d:DesignInstance 标记扩展中,指定要创建的类型。 使用 d:IsDesignTimeCreatable 可以指定是创建某一实例或您的类型还是设计器生成的替代类型。

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

访问设计时特性

可以通过 https://schemas.microsoft.com/expression/blend/2008 命名空间访问设计时特性。 当您在“设计”视图中单击 MainWindow 右下角处的根大小标记 (根大小标记) 时,系统会自动映射命名空间。

生成操作

若要启用 d:DesignData,应当针对包含示例数据的 XAML 文件设置生成操作。 下表描述了生成操作。 有关更多信息,请参见演练:使用 WPF 设计器中的示例数据

生成操作

说明

DesignData

在示例数据类型无法创建或具有您要为示例数据值定义的只读属性时,使用此生成操作。 WPF 和 Silverlight 设计器创建与业务对象类型具有相同属性的替换类型。 您的类型不一定是可创建的。 这消除了工厂方法、抽象类型和数据库连接的复杂性。 可以为只读属性赋值。

DesignDataWithDesignTimeCreatableTypes

在可通过使用其默认的空构造函数创建示例数据类型时,使用此生成操作。 WPF and Silverlight Designer 可创建您在示例数据文件中定义的类型的实例。 您所用的类型在 XAML 中必须是可创建的。

请参见

任务

演练:使用 WPF 设计器创建数据绑定

演练:在设计器中使用 DesignInstance 绑定到数据

其他资源

演练:使用 WPF 设计器中的示例数据