XAML 資源XAML Resources

資源是可在應用程式中不同位置重複使用的物件。A resource is an object that can be reused in different places in your application. 資源的範例包括筆刷和樣式。Examples of resources include brushes and styles. 本概觀說明如何使用中的資源XAMLXAMLThis overview describes how to use resources in XAMLXAML. 您也可以建立及存取資源,使用程式碼,或是交換使用程式碼之間和Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)You can also create and access resources by using code, or interchangeably between code and Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML). 如需詳細資訊,請參閱 < 資源和程式碼For more information, see Resources and Code.

注意

本主題中所述的資源檔案為不同的資源檔中所述WPF 應用程式資源、 內容和資料檔案也不同於中所述的內嵌或連結資源管理應用程式資源 (.NET)The resource files described in this topic are different than the resource files described in WPF Application Resource, Content, and Data Files and different than the embedded or linked resources described in Manage Application Resources (.NET).

在 XAML 中使用資源Using Resources in XAML

下列範例會定義SolidColorBrush頁面的根項目上的資源。The following example defines a SolidColorBrush as a resource on the root element of a page. 此範例會參考資源,並使用它來設定屬性的數個子項目,包括Ellipse,則TextBlock,和ButtonThe example then references the resource and uses it to set properties of several child elements, including an Ellipse, a TextBlock, and a Button.

<Page Name="root"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
  <Page.Resources>
    <SolidColorBrush x:Key="MyBrush" Color="Gold"/>
    <Style TargetType="Border" x:Key="PageBackground">
      <Setter Property="Background" Value="Blue"/>
    </Style>
    <Style TargetType="TextBlock" x:Key="TitleText">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="DockPanel.Dock" Value="Top"/>
      <Setter Property="FontSize" Value="18"/>
      <Setter Property="Foreground" Value="#4E87D4"/>
      <Setter Property="FontFamily" Value="Trebuchet MS"/>
      <Setter Property="Margin" Value="0,40,10,10"/>
    </Style>
    <Style TargetType="TextBlock" x:Key="Label">
      <Setter Property="DockPanel.Dock" Value="Right"/>
      <Setter Property="FontSize" Value="8"/>
      <Setter Property="Foreground" Value="{StaticResource MyBrush}"/>
      <Setter Property="FontFamily" Value="Arial"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="Margin" Value="0,3,10,0"/>
    </Style>
  </Page.Resources>
  <StackPanel>
    <Border Style="{StaticResource PageBackground}">
      <DockPanel>
        <TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
        <TextBlock Style="{StaticResource Label}">Label</TextBlock>
        <TextBlock DockPanel.Dock="Top" HorizontalAlignment="Left" FontSize="36" Foreground="{StaticResource MyBrush}" Text="Text" Margin="20" />
        <Button DockPanel.Dock="Top" HorizontalAlignment="Left" Height="30" Background="{StaticResource MyBrush}" Margin="40">Button</Button>
        <Ellipse DockPanel.Dock="Top" HorizontalAlignment="Left" Width="100" Height="100" Fill="{StaticResource MyBrush}" Margin="40" />
      </DockPanel>
    </Border>
  </StackPanel>
</Page>


每個架構層級項目 (FrameworkElement或是FrameworkContentElement) 已Resources屬性,這是包含資源的屬性 (作為ResourceDictionary) 資源定義。Every framework-level element (FrameworkElement or FrameworkContentElement) has a Resources property, which is the property that contains the resources (as a ResourceDictionary) that a resource defines. 您可以在任何項目上定義資源。You can define resources on any element. 不過,資源最常定義於根項目,也就是Page在範例中。However, resources are most often defined on the root element, which is Page in the example.

資源字典中的每個資源都必須有唯一索引鍵。Each resource in a resource dictionary must have a unique key. 當您在標記中定義的資源時,您將指派到唯一的索引鍵X:key 指示詞When you define resources in markup, you assign the unique key through the x:Key Directive. 索引鍵通常是字串;不過,您也可以使用適當的標記延伸,將它設定為其他物件類型。Typically, the key is a string; however, you can also set it to other object types by using the appropriate markup extensions. 特定的功能區中所使用資源非字串索引鍵WPFWPF,特別是代表樣式、 元件資源和資料樣式。Nonstring keys for resources are used by certain feature areas in WPFWPF, notably for styles, component resources, and data styling.

定義資源之後,您可以使用指定索引鍵名稱的資源標記延伸語法,參考要用於屬性值的資源,例如:After you define a resource, you can reference the resource to be used for a property value by using a resource markup extension syntax that specifies the key name, for example:

<Button Background="{StaticResource MyBrush}"/>
<Ellipse Fill="{StaticResource MyBrush}"/>

在上述範例中,當XAMLXAML載入器處理值{StaticResource MyBrush}forBackground屬性Button,資源查閱邏輯會先檢查資源字典中的Button項目。In the preceding example, when the XAMLXAML loader processes the value {StaticResource MyBrush} for the Background property on Button, the resource lookup logic first checks the resource dictionary for the Button element. 如果Button沒有定義資源索引鍵MyBrush(它沒有; 其資源集合是空的),查閱會接著檢查的父項目Button,也就是PageIf Button does not have a definition of the resource key MyBrush (it does not; its resource collection is empty), the lookup next checks the parent element of Button, which is Page. 因此,當您定義的資源上Page根項目,邏輯樹狀結構中的所有項目Page可以存取它,並設定任何屬性的值可接受的您可以重複使用相同的資源Type的資源表示。Thus, when you define a resource on the Page root element, all the elements in the logical tree of the Page can access it, and you can reuse the same resource for setting the value of any property that accepts the Type that the resource represents. 在上述範例中,相同MyBrush資源設定兩個不同的屬性:BackgroundButton,而FillRectangleIn the previous example, the same MyBrush resource sets two different properties: the Background of a Button, and the Fill of a Rectangle.

靜態和動態資源Static and Dynamic Resources

資源可以當做靜態資源或動態資源參考。A resource can be referenced as either a static resource or a dynamic resource. 這是藉由使用StaticResource 標記延伸DynamicResource 標記延伸This is done by using either the StaticResource Markup Extension or the DynamicResource Markup Extension. 標記延伸是一項功能XAMLXAML,您可以讓標記延伸處理屬性字串,並傳回物件,以指定的物件參考XAMLXAML載入器。A markup extension is a feature of XAMLXAML whereby you can specify an object reference by having the markup extension process the attribute string and return the object to a XAMLXAML loader. 如需標記延伸行為的詳細資訊,請參閱標記延伸和 WPF XAMLFor more information about markup extension behavior, see Markup Extensions and WPF XAML.

當您使用標記延伸時,您通常會提供一或多個字串形式的參數,以供該特定標記延伸處理,而不是在所設定的屬性內容中進行評估。When you use a markup extension, you typically provide one or more parameters in string form that are processed by that particular markup extension, rather than being evaluated in the context of the property being set. StaticResource 標記延伸處理按鍵,藉由查閱在所有可用的資源字典中該索引鍵的值。The StaticResource Markup Extension processes a key by looking up the value for that key in all available resource dictionaries. 這會在載入期間發生,也就是載入程序需要指派接受靜態資源參考的屬性值時。This happens during loading, which is the point in time when the loading process needs to assign the property value that takes the static resource reference. DynamicResource 標記延伸改為處理程序藉由建立運算式和該運算式的索引鍵會保持未評估直到實際執行應用程式時,此時運算式會評估並提供值。The DynamicResource Markup Extension instead processes a key by creating an expression, and that expression remains unevaluated until the application is actually run, at which time the expression is evaluated and provides a value.

當您參考資源時,下列考量可能會影響您使用靜態資源參考或動態資源參考:When you reference a resource, the following considerations can influence whether you use a static resource reference or a dynamic resource reference:

  • 如何為您的應用程式來建立資源的整體設計 (每頁、 在應用程式,在鬆散XAMLXAML,在資源專用組件中)。The overall design of how you create the resources for your application (per page, in the application, in loose XAMLXAML, in a resource only assembly).

  • 應用程式功能︰應用程式是否需要即時更新資源?The application functionality: is updating resources in real time part of your application requirements?

  • 該資源參考型別的相關查閱行為。The respective lookup behavior of that resource reference type.

  • 特定屬性或資源類型,以及這些類型的原生行為。The particular property or resource type, and the native behavior of those types.

靜態資源Static Resources

靜態資源參考最適用於下列情況:Static resource references work best for the following circumstances:

  • 應用程式設計將其大部分資源集中到頁面或應用程式層級資源字典中。Your application design concentrates most of all of its resources into page or application level resource dictionaries. 由於靜態資源參考不會根據執行階段行為 (例如重新載入頁面) 重新評估,因此當根據資源和應用程式設計不需要動態資源參考時,可避免使用大量動態資源參考,藉此提升一些效能。Static resource references are not reevaluated based on runtime behaviors such as reloading a page, and therefore there can be some performance benefit to avoiding large numbers of dynamic resource references when they are not necessary per your resource and application design.

  • 您要設定不在屬性的值DependencyObjectFreezableYou are setting the value of a property that is not on a DependencyObject or a Freezable.

  • 您要建立編譯成 DLL 並封裝成應用程式一部分或在應用程式之間共用的資源字典。You are creating a resource dictionary that will be compiled into a DLL, and packaged as part of the application or shared between applications.

  • 您要建立自訂控制項的佈景主題,然後定義用於佈景主題的資源。You are creating a theme for a custom control, and are defining resources that are used within the themes. 在此情況下,您通常不需要動態資源參考查閱行為,而需要靜態資源參考行為,以便佈景主題可預測並內封查閱。For this case, you typically do not want the dynamic resource reference lookup behavior, you instead want the static resource reference behavior so that the lookup is predictable and self-contained to the theme. 使用動態資源參考時,甚至是佈景主題內的參考也會在執行階段之前保持未評估,在套用佈景主題時,有些區域項目可能會重新定義佈景主題嘗試參考的索引鍵,而且區域項目將會落在查閱中的佈景主題本身之前。With a dynamic resource reference, even a reference within a theme is left unevaluated until runtime, and there is a chance that when the theme is applied, some local element will redefine a key that your theme is trying to reference, and the local element will fall prior to the theme itself in the lookup. 如果發生此情況,您的佈景主題將不會如預期般運作。If that happens, your theme will not behave in an expected manner.

  • 您要使用資源來設定大量相依性屬性。You are using resources to set large numbers of dependency properties. 相依性屬性具有屬性系統已啟用的有效值快取,因此如果您將值提供給可在載入時評估的相依性屬性,相依性屬性不必檢查是否有重新評估的運算式,而且可能傳回最後一個有效值。Dependency properties have effective value caching as enabled by the property system, so if you provide a value for a dependency property that can be evaluated at load time, the dependency property does not have to check for a reevaluated expression and can return the last effective value. 這項技術可能會提升效能。This technique can be a performance benefit.

  • 您想要變更的基礎資源的所有取用者,或您想要使用不同的可寫入執行個體維護每個取用者x: 共用屬性You want to change the underlying resource for all consumers, or you want to maintain separate writable instances for each consumer by using the x:Shared Attribute.

靜態資源查閱行為Static resource lookup behavior

  1. 查閱程序會檢查設定屬性之項目所定義的資源字典內,是否有要求的索引鍵。The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.

  2. 查閱程序接著會在邏輯樹狀結構中,向上周游到父項目及其資源字典。The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. 這會繼續直到到達根項目。This continues until the root element is reached.

  3. 接下來,會檢查應用程式資源。Next, application resources are checked. 應用程式資源則由定義資源字典內Application物件您WPFWPF應用程式。Application resources are those resources within the resource dictionary that is defined by the Application object for your WPFWPF application.

資源字典內的靜態資源參考必須參考資源參考之前已在語彙上定義的資源。Static resource references from within a resource dictionary must reference a resource that has already been defined lexically before the resource reference. 靜態資源參考無法解析向前參考。Forward references cannot be resolved by a static resource reference. 因此,如果您使用靜態資源參考,您必須設計資源字典結構,讓預期要供某個資源使用的所有資源都在每個相關資源字典開頭或附近定義。For this reason, if you use static resource references, you must design your resource dictionary structure such that resources intended for by-resource use are defined at or near the beginning of each respective resource dictionary.

靜態資源查閱可以延伸到佈景主題或系統資源,但這只是因為XAMLXAML載入器會延遲要求。Static resource lookup can extend into themes, or into system resources, but this is supported only because the XAMLXAML loader defers the request. 為了將頁面載入時的執行階段佈景主題正確套用至應用程式,必須有此延遲。The deferral is necessary so that the runtime theme at the time the page loads applies properly to the application. 不過,不建議已知只存在於佈景主題中或作為系統資源的索引鍵有靜態資源參考。However, static resource references to keys that are known to only exist in themes or as system resources are not recommended. 這是因為如果使用者即時變更佈景主題,不會重新評估這類參考。This is because such references are not reevaluated if the theme is changed by the user in realtime. 當您要求佈景主題或系統資源時,動態資源參考會更可靠。A dynamic resource reference is more reliable when you request theme or system resources. 但佈景主題項目本身要求另一個資源時則例外。The exception is when a theme element itself requests another resource. 這些參考應該是靜態資源參考,原因如前所述。These references should be static resource references, for the reasons mentioned earlier.

找不到靜態資源參考時的例外狀況行為會有所不同。The exception behavior if a static resource reference is not found varies. 如果已延遲資源,則會在執行階段發生例外狀況。If the resource was deferred, then the exception occurs at runtime. 如果未延遲資源,則會在載入時發生例外狀況。If the resource was not deferred, the exception occurs at load time.

動態資源Dynamic Resources

動態資源最適用於下列情況:Dynamic resources work best for the following circumstances:

  • 資源的值取決於執行階段之後才知道的狀況。The value of the resource depends on conditions that are not known until runtime. 這包括系統資源或使用者可設定的資源。This includes system resources, or resources that are otherwise user settable. 例如,您可以建立參考系統屬性的 setter 值所公開之SystemColorsSystemFonts,或SystemParametersFor example, you can create setter values that refer to system properties, as exposed by SystemColors, SystemFonts, or SystemParameters. 由於這些值最終是來自使用者和作業系統的執行階段環境,因此是真正動態的。These values are truly dynamic because they ultimately come from the runtime environment of the user and operating system. 您也可能有會變更的應用程式層級佈景主題,其中的頁面層級資源存取也必須擷取該變更。You might also have application-level themes that can change, where page-level resource access must also capture the change.

  • 您要建立或參考自訂控制項的佈景主題樣式。You are creating or referencing theme styles for a custom control.

  • 您想要調整的內容ResourceDictionary應用程式存留期間。You intend to adjust the contents of a ResourceDictionary during an application lifetime.

  • 您有內含相互依存性的複雜資源結構,可能需要向前參考。You have a complicated resource structure that has interdependencies, where a forward reference may be required. 靜態資源參考不支援向前參考,但動態資源參考則支援,因為資源不需要執行階段之前評估,因此向前參考並不相關的概念。Static resource references do not support forward references, but dynamic resource references do support them because the resource does not need to be evaluated until runtime, and forward references are therefore not a relevant concept.

  • 您所參考的資源從編譯或工作集的觀點來看特別大,因此當頁面載入時,可能無法立即使用資源。You are referencing a resource that is particularly large from the perspective of a compile or working set, and the resource might not be used immediately when the page loads. 靜態資源參考一律從載入XAMLXAML當頁面載入; 不過,動態資源參考不會載入直到實際使用。Static resource references always load from XAMLXAML when the page loads; however, a dynamic resource reference does not load until it is actually used.

  • 您要建立樣式,其中的 setter 值可能來自受到佈景主題或其他使用者設定影響的其他值。You are creating a style where setter values might come from other values that are influenced by themes or other user settings.

  • 您要將資源套用至可能在應用程式存留期間於邏輯樹狀結構中重設父代的項目。You are applying resources to elements that might be reparented in the logical tree during application lifetime. 變更父代也可能會變更資源查閱範圍,因此如果您想要根據新範圍重新評估重設父代項目的資源,請一律使用動態資源參考。Changing the parent also potentially changes the resource lookup scope, so if you want the resource for a reparented element to be reevaluated based on the new scope, always use a dynamic resource reference.

動態資源查閱行為Dynamic resource lookup behavior

動態資源參考的資源查閱行為平行程式碼中的查閱行為,如果您呼叫FindResourceSetResourceReferenceResource lookup behavior for a dynamic resource reference parallels the lookup behavior in your code if you call FindResource or SetResourceReference.

  1. 查閱程序會檢查設定屬性之項目所定義的資源字典內,是否有要求的索引鍵。The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.

  2. 查閱程序接著會在邏輯樹狀結構中,向上周游到父項目及其資源字典。The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. 這會繼續直到到達根項目。This continues until the root element is reached.

  3. 接下來,會檢查應用程式資源。Next, application resources are checked. 應用程式資源則由定義資源字典內Application物件您WPFWPF應用程式。Application resources are those resources within the resource dictionary that is defined by the Application object for your WPFWPF application.

  4. 檢查目前使用中佈景主題的佈景主題資源字典。Theme resource dictionary is checked, for the currently active theme. 如果佈景主題在執行階段變更,則會重新評估其值。If the theme changes at runtime, the value is reevaluated.

  5. 檢查系統資源。System resources are checked.

例外狀況行為 (如果有的話) 則有所不同:Exception behavior (if any) varies:

  • 如果所要求資源FindResource呼叫,並找不到,會引發例外狀況。If a resource was requested by a FindResource call, and was not found, an exception is raised.

  • 如果所要求資源TryFindResource呼叫,並找不到,會引發任何例外狀況,但傳回的值是nullIf a resource was requested by a TryFindResource call, and was not found, no exception is raised, but the returned value is null. 如果要設定的屬性不接受null,則仍有可能,會引發更深入的例外狀況 (這取決於所設定的個別屬性)。If the property being set does not accept null, then it is still possible that a deeper exception will be raised (this depends on the individual property being set).

  • 如果中的動態資源參考要求資源XAMLXAML,並找不到,則其行為取決於一般屬性系統中,但一般行為會如同資源所在的層級發生任何屬性設定作業。If a resource was requested by a dynamic resource reference in XAMLXAML, and was not found, then the behavior depends on the general property system, but the general behavior is as if no property setting operation occurred at the level where the resource exists. 例如,如果您嘗試使用無法評估的資源來設定個別按鈕項目的背景,則沒有值會設定結果,但有效值仍然可能來自屬性系統和值優先順序中的其他參與值。For instance, if you attempt to set the background on a an individual button element using a resource that could not be evaluated, then no value set results, but the effective value can still come from other participants in the property system and value precedence. 例如,背景值仍然可能來自本機定義的按鈕樣式,或來自佈景主題樣式。For instance, the background value might still come from a locally defined button style, or from the theme style. 針對不是由佈景主題樣式定義的屬性,資源評估失敗後的有效值可能來自屬性中繼資料內的預設值。For properties that are not defined by theme styles, the effective value after a failed resource evaluation might come from the default value in the property metadata.

限制Restrictions

動態資源參考有一些值得注意的限制。Dynamic resource references have some notable restrictions. 至少必須符合下列其中一個情況:At least one of the following must be true:

由於所設定的屬性必須是DependencyPropertyFreezable屬性,大部分的屬性變更可以傳播至 UI,因為屬性變更 (變更的動態資源值) 由屬性系統認可。Because the property being set must be a DependencyProperty or Freezable property, most property changes can propagate to UI because a property change (the changed dynamic resource value) is acknowledged by the property system. 大多數控制項所包含邏輯,會強制另一個控制項的配置,如果DependencyProperty變更和屬性可能會影響版面配置。Most controls include logic that will force another layout of a control if a DependencyProperty changes and that property might affect layout. 然而,並非所有的屬性具有DynamicResource 標記延伸做為其值會保證提供的方式,更新在 UI 中的即時值。However, not all properties that have a DynamicResource Markup Extension as their value are guaranteed to provide the value in such a way that they update in realtime in the UI. 該功能仍然可能會依屬性、擁有屬性的類型,甚至是應用程式的邏輯結構而有所不同。That functionality still might vary depending on the property, as well as depending on the type that owns the property, or even the logical structure of your application.

樣式、DataTemplate 和隱含索引鍵Styles, DataTemplates, and Implicit Keys

更早版本,說明所有項目中ResourceDictionary必須有索引鍵。Earlier, it was stated that all items in a ResourceDictionary must have a key. 不過,這不表示所有資源都必須都有明確x:KeyHowever, that does not mean that all resources must have an explicit x:Key. 有幾種物件類型會在定義為資源時支援隱含索引鍵,其中索引鍵值會繫結至另一個屬性的值。Several object types support an implicit key when defined as a resource, where the key value is tied to the value of another property. 這就所謂的隱含索引鍵,而x:Key屬性是明確的索引鍵。This is known as an implicit key, whereas an x:Key attribute is an explicit key. 您可以藉由指定明確索引鍵,來覆寫任何隱含索引鍵。You can overwrite any implicit key by specifying an explicit key.

資源的其中一個非常重要的案例是當您定義StyleOne very important scenario for resources is when you define a Style. 事實上,Style幾乎都定義為資源字典中的項目因為樣式原本就是僅供重複使用。In fact, a Style is almost always defined as an entry in a resource dictionary, because styles are inherently intended for reuse. 如需有關樣式的詳細資訊,請參閱 < 設定樣式和範本For more information about styles, see Styling and Templating.

控制項的樣式可以透過隱含索引鍵來建立及參考。Styles for controls can be both created with and referenced with an implicit key. 定義預設控制項外觀的佈景主題樣式會依賴此隱含索引鍵。The theme styles that define the default appearance of a control rely on this implicit key. 從要求它的觀點來看,隱含索引鍵是Type控制項本身。The implicit key from the standpoint of requesting it is the Type of the control itself. 從定義資源的觀點來看,隱含索引鍵是TargetType的樣式。The implicit key from the standpoint of defining the resource is the TargetType of the style. 因此,如果您要建立自訂控制項的佈景主題,建立互動的樣式與現有佈景主題樣式,您不需要指定X:key 指示詞StyleTherefore, if you are creating themes for custom controls, creating styles that interact with existing theme styles, you do not need to specify an x:Key Directive for that Style. 此外,如果您想要使用佈景主題樣式,則完全不需要指定任何樣式。And if you want to use the themed styles, you do not need to specify any style at all. 比方說,下列樣式定義的運作方式,即使Style有索引鍵,似乎不到資源:For instance, the following style definition works, even though the Style resource does not appear to have a key:

<Style TargetType="Button">
  <Setter Property="Background">
    <Setter.Value>
      <LinearGradientBrush>
        <GradientStop Offset="0.0" Color="AliceBlue"/>
        <GradientStop Offset="1.0" Color="Salmon"/>           
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>  
  <Setter Property="FontSize" Value="18"/>
</Style>

該樣式其實有索引鍵: 隱含索引鍵typeof( Button )That style really does have a key: the implicit key typeof(Button). 在標記中,您可以指定TargetType直接做為類型名稱 (或您可以選擇性地使用{x: Type...}In markup, you can specify a TargetType directly as the type name (or you can optionally use {x:Type...} 要傳回Typeto return a Type.

透過所使用的預設佈景主題樣式機制WPFWPF,樣式會套用作為執行階段樣式Button在頁面上,即使Button本身不會嘗試指定其Style屬性或特定的資源參考的樣式。Through the default theme style mechanisms used by WPFWPF, that style is applied as the runtime style of a Button on the page, even though the Button itself does not attempt to specify its Style property or a specific resource reference to the style. 稍早在比使用佈景主題字典樣式所具有的相同金鑰的佈景主題字典樣式的查閱序列中找到您頁面中定義的樣式。Your style defined in the page is found earlier in the lookup sequence than the theme dictionary style, using the same key that the theme dictionary style has. 您可以指定<Button>Hello</Button>頁面,並以您定義的樣式中的任何地方TargetTypeButton會套用至該按鈕。You could just specify <Button>Hello</Button> anywhere in the page, and the style you defined with TargetType of Button would apply to that button. 如果您想,您可以仍然明確樣式的索引鍵具有相同的型別值TargetType、 清楚的標記,但為選用。If you want, you can still explicitly key the style with the same type value as TargetType, for clarity in your markup, but that is optional.

樣式的隱含索引鍵不會套用在控制項上如果OverridesDefaultStyletrue(另請注意,OverridesDefaultStyle可能設定為 control 類別,而不是明確的控制項執行個體上的原生行為的一部分)。Implicit keys for styles do not apply on a control if OverridesDefaultStyle is true (also note that OverridesDefaultStyle might be set as part of native behavior for the control class, rather than explicitly on an instance of the control). 此外,為了支援衍生的類別情節的隱含索引鍵,控制項必須覆寫DefaultStyleKey(提供做為一部分的所有現有控制項WPFWPF這麼做)。Also, in order to support implicit keys for derived class scenarios, the control must override DefaultStyleKey (all existing controls provided as part of WPFWPF do this). 如需樣式、 佈景主題和控制設計的詳細資訊,請參閱設計可設定樣式控制項的方針For more information about styles, themes, and control design, see Guidelines for Designing Stylable Controls.

DataTemplate 也有隱含索引鍵。DataTemplate also has an implicit key. 隱含索引鍵DataTemplateDataType屬性值。The implicit key for a DataTemplate is the DataType property value. DataType 也可以指定為型別名稱,而不是使用明確{x: Type...}.DataType can also be specified as the name of the type rather than explicitly using {x:Type...}. 如需詳細資訊,請參閱 < 資料範本化概觀For details, see Data Templating Overview.

另請參閱See also