共用方式為


設計階段屬性

當您使用 WPF Designer for Visual Studio 建置 WPF 或 Silverlight 應用程式時,有時候需要提供資訊才能讓 [設計] 檢視正確運作。 您可以使用「設計階段屬性」(Design-Time Attribute) 來指定這種資訊。例如,設計階段屬性能夠讓您以特定值調整根視窗大小來容納配置設計,同時保留在執行階段依內容調整大小的功能。 編譯期間會忽略設計階段屬性,而且這些屬性在執行階段也不會有任何作用。

設計階段屬性

WPF Designer提供下列設計階段屬性。

設計階段屬性

描述

使用方式範例

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 檔案,檔案中包含要在設計階段使用的範例資料。 請使用 DesignDataDesignDataWithDesignTimeCreatableTypes 建置動作來整合範例資料與專案。 您可以為唯讀屬性指派值。 如需詳細資訊,請參閱逐步解說:使用 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 Designer 會建立與您的商務物件型別具有相同屬性的替代型別。 您的型別不需要是可建立的型別。 這可消除有關 Factory 方法、抽象型別和資料庫連接的複雜工作。 您可以為唯讀屬性指派值。

DesignDataWithDesignTimeCreatableTypes

當範例資料型別可以使用其預設空白建構函式來建立時,請使用這個建置動作。 WPF 和 Silverlight Designer 會建立範例資料檔中定義之型別的執行個體。 您的型別必須可以在 XAML 中建立。

請參閱

工作

逐步解說:使用 WPF 設計工具建立資料繫結

逐步解說:使用 DesignInstance 繫結至設計工具中的資料

逐步解說:使用 WPF 設計工具中的範例資料