樣式設定和範本化Styling and Templating

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 樣式設定和範本化係指一套功能 (樣式、範本、觸發程序及分鏡腳本),可讓開發人員和設計人員創造引人注目的效果,並為其產品建立一致的外觀。styling and templating refer to a suite of features (styles, templates, triggers, and storyboards) that allow developers and designers to create visually compelling effects and to create a consistent appearance for their product. 雖然開發人員和 (或) 設計人員可以依個別應用程式廣泛地自訂外觀,但強大的樣式設定和範本化模型仍有其必要性,這可允許在應用程式內或應用程式之間維護及共用外觀。Although developers and or designers can customize the appearance extensively on an application-by-application basis, a strong styling and templating model is necessary to allow maintenance and sharing of the appearance within and among applications. Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 便有提供該模型。provides that model.

WPFWPF 樣式設定模型的另一項功能是將呈現與邏輯區分開來。Another feature of the WPFWPF styling model is the separation of presentation and logic. 這意謂著設計人員可以在開發人員使用 C# 或 Visual Basic 來處理程式設計邏輯的同時,僅使用 XAMLXAML 來處理應用程式的外觀。This means that designers can work on the appearance of an application by using only XAMLXAML at the same time that developers work on the programming logic by using C# or Visual Basic.

本概觀將焦點放在應用程式的樣式設定和範本化方面,而不討論任何資料繫結概念。This overview focuses on the styling and templating aspects of the application and does not discuss any data binding concepts. 如需有關資料繫結的資訊,請參閱資料繫結概觀For information about data binding, see Data Binding Overview.

此外,了解資源相當重要,資源可讓您重複使用樣式和範本。In addition, it is important to understand resources, which are what enable styles and templates to be reused. 如需有關資源的詳細資訊,請參閱 XAML 資源For more information about resources, see XAML Resources.

樣式設定和範本化範例Styling and Templating Sample

本概觀中使用的程式碼範例是根據下圖中所示的簡單相片範例:The code examples used in this overview are based on a simple photo sample shown in the following illustration:

已設定樣式的 ListViewStyled ListView

這個簡單的相片範例使用樣式設定和範本化,來創造引人注目的使用者體驗。This simple photo sample uses styling and templating to create a visually compelling user experience. 此範例有兩個TextBlock項目和ListBox控制項繫結至映像的清單。The sample has two TextBlock elements and a ListBox control that is bound to a list of images. 如需完整範例,請參閱樣式設定和範本化範例簡介 (英文)For the complete sample, see Introduction to Styling and Templating Sample.

樣式基本概念Style Basics

您可以將Style作為便利的方式來將一組屬性值套用至一個以上的項目。You can think of a Style as a convenient way to apply a set of property values to more than one element. 例如,請考慮下列TextBlock項目和其預設外觀:For example, consider the following TextBlock elements and their default appearance:

<TextBlock>My Pictures</TextBlock>
<TextBlock>Check out my new pictures!</TextBlock>

樣式設定範例螢幕擷取畫面Styling sample screenshot

您可以藉由設定屬性,例如變更預設外觀FontSizeFontFamily,在每個TextBlock直接項目。You can change the default appearance by setting properties, such as FontSize and FontFamily, on each TextBlock element directly. 不過,如果您想要您TextBlock元素共用某些屬性,您可以建立StyleResources一節您XAMLXAML檔案,如下所示:However, if you want your TextBlock elements to share some properties, you can create a Style in the Resources section of your XAMLXAML file, as shown here:

<Window.Resources>
<!--A Style that affects all TextBlocks-->
<Style TargetType="TextBlock">
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontFamily" Value="Comic Sans MS"/>
  <Setter Property="FontSize" Value="14"/>
</Style>
</Window.Resources>

當您設定TargetType您要的樣式TextBlock型別,樣式會套用到所有TextBlock 視窗中的項目。When you set the TargetType of your style to the TextBlock type, the style is applied to all the TextBlock elements in the window.

現在TextBlock項目會出現,如下所示:Now the TextBlock elements appear as follows:

樣式設定範例螢幕擷取畫面Styling sample screenshot

延伸樣式Extending Styles

或許您想要兩個TextBlock元素共用某些屬性值,例如FontFamily和置中對齊HorizontalAlignment,但您也想要有一些其他屬性的"My Pictures"文字。Perhaps you want your two TextBlock elements to share some property values, such as the FontFamily and the centered HorizontalAlignment, but you also want the text "My Pictures" to have some additional properties. 您可以建立一個以第一個樣式為基礎的新樣式來達到該目的,如以下所示:You can do that by creating a new style that is based on the first style, as shown here:

<Window.Resources>
<!--A Style that extends the previous TextBlock Style-->
<!--This is a "named style" with an x:Key of TitleText-->
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
       TargetType="TextBlock"
       x:Key="TitleText">
  <Setter Property="FontSize" Value="26"/>
  <Setter Property="Foreground">
  <Setter.Value>
      <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
        <LinearGradientBrush.GradientStops>
          <GradientStop Offset="0.0" Color="#90DDDD" />
          <GradientStop Offset="1.0" Color="#5BFFFF" />
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
</Style>
</Window.Resources>

請注意,前一個樣式已有指定的 x:KeyNotice that the previous style is given an x:Key. 若要套用樣式,您設定Style屬性上的您TextBlockx:Key值,如下所示:To apply the style, you set the Style property on your TextBlock to the x:Key value, as shown here:

<TextBlock Style="{StaticResource TitleText}" Name="textblock1">My Pictures</TextBlock>
<TextBlock>Check out my new pictures!</TextBlock>

TextBlock樣式現在已HorizontalAlignment的值Center,則FontFamily的值Comic Sans MSFontSize值為 26,和Foreground值設定為LinearGradientBrush範例所示。This TextBlock style now has a HorizontalAlignment value of Center, a FontFamily value of Comic Sans MS, a FontSize value of 26, and a Foreground value set to the LinearGradientBrush shown in the example. 請注意,它會覆寫FontSize的基底的樣式值。Notice that it overrides the FontSize value of the base style. 如果有一個以上Setter中設定相同的屬性Style,則Setter也就是宣告最後一個會優先採用。If there is more than one Setter setting the same property in a Style, the Setter that is declared last takes precedence.

下圖顯示什麼TextBlock元素現在看起來像:The following shows what the TextBlock elements now look like:

已設定樣式的 TextBlockStyled TextBlocks

TitleText樣式會擴充已建立的樣式TextBlock型別。This TitleText style extends the style that has been created for the TextBlock type. 您也可以使用 x:Key 值來延伸具有 x:Key 的樣式。You can also extend a style that has an x:Key by using the x:Key value. 如需範例,請參閱針對提供的範例BasedOn屬性。For an example, see the example provided for the BasedOn property.

TargetType 屬性 (Property) 與 x:Key 屬性 (Attribute) 的關聯性Relationship of the TargetType Property and the x:Key Attribute

第一個範例所示,設定TargetType屬性,以TextBlock而不為樣式指派x:Key會導致將樣式套用至所有TextBlock項目。As shown in the first example, setting the TargetType property to TextBlock without assigning the style an x:Key causes the style to be applied to all TextBlock elements. 在此情況下,x:Key 會隱含地設定為 {x:Type TextBlock}In this case, the x:Key is implicitly set to {x:Type TextBlock}. 這表示,如果您明確設定x:Key以外的其他值的任何項目{x:Type TextBlock},則Style不會套用到所有TextBlock項目自動。This means that if you explicitly set the x:Key value to anything other than {x:Type TextBlock}, the Style is not applied to all TextBlock elements automatically. 相反地,您必須套用的樣式 (使用x:Key值) 到TextBlock項目明確。Instead, you must apply the style (by using the x:Key value) to the TextBlock elements explicitly. 如果您的樣式是資源區段中,您未設定TargetType屬性,您的樣式,則您必須提供x:KeyIf your style is in the resources section and you do not set the TargetType property on your style, then you must provide an x:Key.

除了提供的預設值x:Key,則TargetType屬性會指定要套用 setter 屬性的型別。In addition to providing a default value for the x:Key, the TargetType property specifies the type to which setter properties apply. 如果您未指定TargetType,您必須限定名稱的屬性,在您Setter使用語法的類別名稱的物件Property="ClassName.Property"If you do not specify a TargetType, you must qualify the properties in your Setter objects with a class name by using the syntax Property="ClassName.Property". 比方說,而不是設定Property="FontSize",您必須設定Property"TextBlock.FontSize""Control.FontSize"For example, instead of setting Property="FontSize", you must set Property to "TextBlock.FontSize" or "Control.FontSize".

另請注意,許多 WPFWPF 控制項都是由其他 WPFWPF 控制項的組合所組成。Also note that many WPFWPF controls consist of a combination of other WPFWPF controls. 如果您建立一個套用到某個型別所有控制項的樣式,可能會獲得非預期的結果。If you create a style that applies to all controls of a type, you might get unexpected results. 比方說,如果您建立目標的樣式TextBlock中輸入Window,樣式會套用到所有TextBlock控制項,在視窗中,即使TextBlock屬於另一個控制項,例如ListBoxFor example, if you create a style that targets the TextBlock type in a Window, the style is applied to all TextBlock controls in the window, even if the TextBlock is part of another control, such as a ListBox.

樣式和資源Styles and Resources

您可以使用衍生自任何項目上的樣式FrameworkElementFrameworkContentElementYou can use a style on any element that derives from FrameworkElement or FrameworkContentElement. 宣告樣式的最常見方式是在 XAMLXAML 檔案的 Resources 區段中宣告成資源,如先前的範例所示。The most common way to declare a style is as a resource in the Resources section in a XAMLXAML file, as shown in the previous examples. 由於樣式是資源,因此它們會遵守適用於所有資源的相同範圍限定規則;您宣告樣式的位置會影響可以套用樣式的位置。Because styles are resources, they obey the same scoping rules that apply to all resources; where you declare a style affects where the style can be applied. 例如,如果您是在應用程式定義 XAMLXAML 檔案的根元素中宣告樣式,就可在應用程式中的任何地方使用該樣式。For example, if you declare the style in the root element of your application definition XAMLXAML file, the style can be used anywhere in your application. 如果您建立一個導覽應用程式並在應用程式的其中一個 XAMLXAML 檔案中宣告樣式,則只能在該 XAMLXAML 檔案中使用該樣式。If you create a navigation application and declare the style in one of the application's XAMLXAML files, the style can be used only in that XAMLXAML file. 如需有關資源範圍限定規則的詳細資訊,請參閱 XAML 資源For more information about scoping rules for resources, see XAML Resources.

此外,您也可以在本概觀稍後的共用的資源和佈景主題中,找到有關樣式和資源的詳細資訊。In addition, you can find more information about styles and resources in Shared Resources and Themes later in this overview.

以程式設計方式設定樣式Setting Styles Programmatically

若要以程式設計方式將具名的樣式指派給項目,獲取的資源集合中的樣式,並將它指派給元素的Style屬性。To assign a named style to an element programmatically, get the style from the resources collection and assign it to the element's Style property. 請注意,資源集合中的項目都屬於型別ObjectNote that the items in a resources collection are of type Object. 因此,您必須轉換為要擷取的樣式Style之前將它指派給Style屬性。Therefore, you must cast the retrieved style to a Style before assigning it to the Style property. 例如,若要設定已定義TitleText上設定樣式TextBlock名為textblock1,執行下列動作:For example, to set the defined TitleText style on a TextBlock named textblock1, do the following:

textblock1.Style = (Style)(this.Resources["TitleText"]);
textblock1.Style = CType(Me.Resources("TitleText"), Style)

請注意,套用樣式之後,系統就會將它密封而無法供您變更。Note that once a style has been applied, it is sealed and cannot be changed. 如果您想要動態變更已經套用的樣式,就必須建立新的樣式來取代現有的樣式。If you want to dynamically change a style that has already been applied, you must create a new style to replace the existing one. 如需詳細資訊,請參閱 IsSealed 屬性 (Property)。For more information, see the IsSealed property.

您可以建立一個物件來根據自訂邏輯選擇要套用的樣式。You can create an object that chooses a style to apply based on custom logic. 如需範例,請參閱針對提供的範例StyleSelector類別。For an example, see the example provided for the StyleSelector class.

繫結、動態資源及事件處理常式Bindings, Dynamic Resources, and Event Handlers

請注意,您可以使用 Setter.Value 屬性來指定 Binding 標記延伸DynamicResource 標記延伸Note that you can use the Setter.Value property to specify a Binding Markup Extension or a DynamicResource Markup Extension. 如需詳細資訊,請參閱提供的範例Setter.Value屬性。For more information, see the examples provided for the Setter.Value property.

到目前為止,本概觀僅討論使用 setter 來設定屬性值。So far, this overview only discusses the use of setters to set property value. 您也可以在樣式中指定事件處理常式。You can also specify event handlers in a style. 如需詳細資訊,請參閱EventSetterFor more information, see EventSetter.

資料範本Data Templates

在此範例應用程式中,沒有ListBox繫結至相片清單的控制項:In this sample application, there is a ListBox control that is bound to a list of photos:

<ListBox ItemsSource="{Binding Source={StaticResource MyPhotos}}"
         Background="Silver" Width="600" Margin="10" SelectedIndex="0"/>

ListBox目前看起來如下所示:This ListBox currently looks like the following:

套用範本之前的 ListBoxListBox before applying template

大多數控制項都有某個型別的內容,而該內容通常來自您要繫結的資料。Most controls have some type of content, and that content often comes from data that you are binding to. 在此範例中,該資料是相片清單。In this sample, the data is the list of photos. WPFWPF,您使用DataTemplate來定義資料的視覺表示法。In WPFWPF, you use a DataTemplate to define the visual representation of data. 基本上,什麼您放入DataTemplate決定資料在轉譯的應用程式中的外觀。Basically, what you put into a DataTemplate determines what the data looks like in the rendered application.

在我們的範例應用程式中,每個自訂的 Photo 物件都有一個字串型別的 Source 屬性,用來指定影像的檔案路徑。In our sample application, each custom Photo object has a Source property of type string that specifies the file path of the image. 目前,相片物件是顯示成檔案路徑。Currently, the photo objects appear as file paths.

在您建立文件,相片才會顯示為映像,DataTemplate做為資源:For the photos to appear as images, you create a DataTemplate as a resource:

<Window.Resources>
<!--DataTemplate to display Photos as images
    instead of text strings of Paths-->
<DataTemplate DataType="{x:Type local:Photo}">
  <Border Margin="3">
    <Image Source="{Binding Source}"/>
  </Border>
</DataTemplate>
</Window.Resources>

請注意,DataType屬性是非常類似於TargetType屬性StyleNotice that the DataType property is very similar to the TargetType property of the Style. 如果您DataTemplate位於 [資源] 區段中,當您指定DataType屬性的型別並不會將它指派x:Key,則DataTemplate該型別出現時套用。If your DataTemplate is in the resources section, when you specify the DataType property to a type and not assign it an x:Key, the DataTemplate is applied whenever that type appears. 您一律可以選擇来指派DataTemplate具有x:Key,然後將它設為StaticResource屬性採用DataTemplate類型,例如ItemTemplate屬性或ContentTemplate屬性。You always have the option to assign the DataTemplate with an x:Key and then set it as a StaticResource for properties that take DataTemplate types, such as the ItemTemplate property or the ContentTemplate property.

基本上,DataTemplate在上述範例中定義了每當有Photo物件,它應該會顯示為ImageBorderEssentially, the DataTemplate in the above example defines that whenever there is a Photo object, it should appear as an Image within a Border. 與這個DataTemplate,我們的應用程式現在看起來像這樣:With this DataTemplate, our application now looks like this:

相片影像Photo image

資料範本化模型還提供其他功能。The data templating model provides other features. 例如,如果您要顯示包含使用其他集合的集合資料HeaderedItemsControl輸入,例如MenuTreeView,沒有HierarchicalDataTemplateFor example, if you are displaying collection data that contains other collections using a HeaderedItemsControl type such as a Menu or a TreeView, there is the HierarchicalDataTemplate. 另一個資料範本化功能是DataTemplateSelector,這可讓您選擇DataTemplate使用根據自訂邏輯。Another data templating feature is the DataTemplateSelector, which allows you to choose a DataTemplate to use based on custom logic. 如需詳細資訊,請參閱資料範本化概觀,其中提供不同資料範本化功能的更深入探討。For more information, see Data Templating Overview, which provides a more in-depth discussion of the different data templating features.

控制項範本Control Templates

WPFWPF,則ControlTemplate控制項的定義控制項的外觀。In WPFWPF, the ControlTemplate of a control defines the appearance of the control. 您可以藉由定義新變更的結構和控制項的外觀ControlTemplate控制項。You can change the structure and appearance of a control by defining a new ControlTemplate for the control. 在許多情況下,這會賦予您足夠的彈性,讓您不需要撰寫自己的自訂控制項。In many cases, this gives you enough flexibility so that you do not have to write your own custom controls. 如需詳細資訊,請參閱透過建立 ControlTemplate 自訂現有控制項的外觀For more information, see Customizing the Appearance of an Existing Control by Creating a ControlTemplate.

觸發程序Triggers

觸發程序會在屬性值發生變更或在某個事件被引發時,設定屬性或啟動動作 (例如動畫)。A trigger sets properties or starts actions, such as an animation, when a property value changes or when an event is raised. StyleControlTemplate,並DataTemplate都有Triggers可以包含一組觸發程序的屬性。Style, ControlTemplate, and DataTemplate all have a Triggers property that can contain a set of triggers. 有各種不同類型的觸發程序。There are various types of triggers.

屬性觸發程序Property Triggers

ATrigger設定屬性值,或啟動屬性的值為基礎的動作稱為屬性觸發程序。A Trigger that sets property values or starts actions based on the value of a property is called a property trigger.

為了示範如何使用屬性觸發程序,您可以讓每個ListBoxItem部分透明,除非已選取。To demonstrate how to use property triggers, you can make each ListBoxItem partially transparent unless it is selected. 下列樣式集Opacity的值ListBoxItem0.5The following style sets the Opacity value of a ListBoxItem to 0.5. IsSelected屬性是true,不過Opacity設定為1.0:When the IsSelected property is true, however, the Opacity is set to 1.0:

<Style TargetType="ListBoxItem">
  <Setter Property="Opacity" Value="0.5" />
  <Setter Property="MaxHeight" Value="75" />
  <Style.Triggers>
    <Trigger Property="IsSelected" Value="True">
        <Setter Property="Opacity" Value="1.0" />
    </Trigger>
  </Style.Triggers>
</Style>

這個範例會使用Trigger若要設定的屬性值,但請注意Trigger類別也有EnterActionsExitActions啟用觸發程序執行動作的屬性。This example uses a Trigger to set a property value, but note that the Trigger class also has the EnterActions and ExitActions properties that enable a trigger to perform actions.

請注意,MaxHeight的屬性ListBoxItem設定為75Notice that the MaxHeight property of the ListBoxItem is set to 75. 在下圖中,第三個項目是已選取的項目:In the following illustration, the third item is the selected item:

已設定樣式的 ListViewStyled ListView

EventTrigger 和分鏡腳本EventTriggers and Storyboards

觸發程序的另一種是EventTrigger,這會啟動一組發生的事件為基礎的動作。Another type of trigger is the EventTrigger, which starts a set of actions based on the occurrence of an event. 例如,下列EventTrigger物件指定當滑鼠指標進入ListBoxItem,則MaxHeight屬性以動畫顯示的值90透過0.2第二段。For example, the following EventTrigger objects specify that when the mouse pointer enters the ListBoxItem, the MaxHeight property animates to a value of 90 over a 0.2 second period. 當滑鼠指標從項目移開時,該屬性會在 1 秒的期間內恢復成原始值。When the mouse moves away from the item, the property returns to the original value over a period of 1 second. 請注意如何不需要指定ToMouseLeave動畫。Note how it is not necessary to specify a To value for the MouseLeave animation. 這是因為動畫能夠記錄原始值。This is because the animation is able to keep track of the original value.

<EventTrigger RoutedEvent="Mouse.MouseEnter">
  <EventTrigger.Actions>
    <BeginStoryboard>
      <Storyboard>
        <DoubleAnimation
          Duration="0:0:0.2"
          Storyboard.TargetProperty="MaxHeight"
          To="90"  />
      </Storyboard>
    </BeginStoryboard>
  </EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
  <EventTrigger.Actions>
    <BeginStoryboard>
      <Storyboard>
        <DoubleAnimation
          Duration="0:0:1"
          Storyboard.TargetProperty="MaxHeight"  />
      </Storyboard>
    </BeginStoryboard>
  </EventTrigger.Actions>
</EventTrigger>

如需詳細資訊,請參閱分鏡腳本概觀For more information, see the Storyboards Overview.

在下圖中,滑鼠指標正指向第三個項目:In the following illustration, the mouse is pointing to the third item:

樣式設定範例螢幕擷取畫面Styling sample screenshot

MultiTrigger、DataTrigger 及 MultiDataTriggerMultiTriggers, DataTriggers, and MultiDataTriggers

除了TriggerEventTrigger,還有其他類型的觸發程序。In addition to Trigger and EventTrigger, there are other types of triggers. MultiTrigger 可讓您設定多個條件為基礎的屬性值。MultiTrigger allows you to set property values based on multiple conditions. 您使用DataTriggerMultiDataTrigger若條件的屬性是資料繫結。You use DataTrigger and MultiDataTrigger when the property of your condition is data-bound.

共用的資源和佈景主題Shared Resources and Themes

典型的 Windows Presentation Foundation (WPF) 應用程式可能會有多個套用整個應用程式的使用者介面 (UI) 資源。A typical Windows Presentation Foundation (WPF) application might have multiple user interface (UI) resources that are applied throughout the application. 這組資源集體可視為應用程式的佈景主題。Collectively, this set of resources can be considered the theme for the application. Windows Presentation Foundation (WPF) 提供的支援封裝使用者介面 (UI) 資源做為主題藉由使用資源字典,其會封裝為ResourceDictionary類別。Windows Presentation Foundation (WPF) provides support for packaging user interface (UI) resources as a theme by using a resource dictionary that is encapsulated as the ResourceDictionary class.

Windows Presentation Foundation (WPF) 佈景主題定義使用的樣式和範本化機制,Windows Presentation Foundation (WPF) 會公開用於自訂的視覺效果的任何項目。Windows Presentation Foundation (WPF) themes are defined by using the styling and templating mechanism that Windows Presentation Foundation (WPF) exposes for customizing the visuals of any element.

Windows Presentation Foundation (WPF) 佈景主題資源會儲存在內嵌的資源字典。Windows Presentation Foundation (WPF) theme resources are stored in embedded resource dictionaries. 這些資源字典必須內嵌在已簽署的組件內,並且可內嵌在與程式碼本身相同的組件中,也可內嵌在並存的組件中。These resource dictionaries must be embedded within a signed assembly, and can either be embedded in the same assembly as the code itself or in a side-by-side assembly. 在 PresentationFramework.dll 中,其中包含 Windows Presentation Foundation (WPF) 控制項的組件的情況下佈景主題資源是一系列並排顯示組件中。In the case of PresentationFramework.dll, the assembly which contains Windows Presentation Foundation (WPF) controls, theme resources are in a series of side-by-side assemblies.

當搜尋元素的樣式時,佈景主題會成為最後一個要查看的地方。The theme becomes the last place to look when searching for the style of an element. 一般而言,搜尋會從元素樹狀目錄往上開始、搜尋適當的資源,然後查看應用程式資源集合,最後查詢系統。Typically, the search will begin by walking up the element tree searching for an appropriate resource, then look in the application resource collection and finally query the system. 這會為應用程式開發人員提供一個機會,可在到達佈景主題之前,先為樹狀目錄或應用程式層級的任何物件,重新定義樣式。This gives application developers a chance to redefine the style for any object at the tree or application level before reaching the theme.

您可以將資源字典定義成個別的檔案,這可讓您跨多個應用程式重複使用佈景主題。You can define resource dictionaries as individual files that enable you to reuse a theme across multiple applications. 您也可以透過定義多個資源字典來提供類型相同但值不同的資源,以建立可切換的佈景主題。You can also create swappable themes by defining multiple resource dictionaries that provide the same types of resources but with different values. 重新定義這些樣式或其他應用程式層級的資源,是簡化應用程式的建議做法。Redefining these styles or other resources at the application level is the recommended approach for skinning an application.

若要共用一組資源,包括樣式和範本,跨應用程式,您可以建立XAMLXAML檔案,並定義ResourceDictionaryTo share a set of resources, including styles and templates, across applications, you can create a XAMLXAML file and define a ResourceDictionary. 例如,請看看下圖,當中顯示部分的使用 ControlTemplates 設定樣式範例For example, take a look at the following illustration that shows part of the Styling with ControlTemplates Sample:

控制項範本範例Control Template Examples

如果您查看範例中的 XAMLXAML 檔案,您將會發現這些檔案都具有下列內容:If you look at the XAMLXAML files in the sample, you will notice that the files all have the following:

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="Shared.xaml" />
</ResourceDictionary.MergedDictionaries>

它是共用shared.xaml,其定義ResourceDictionary,其中包含一組樣式和筆刷資源,可讓擁有一致的外觀範例中的控制項。It is the sharing of shared.xaml, which defines a ResourceDictionary that contains a set of style and brush resources that enables the controls in the sample to have a consistent look.

如需詳細資訊,請參閱合併的資源字典For more information, see Merged Resource Dictionaries.

如果您要為自訂控制項建立佈景主題,請參閱控制項撰寫概觀的<外部控制項程式庫>一節。If you are creating a theme for you custom control, see the External Control Library section of the Control Authoring Overview.

另請參閱See also