相依性屬性概觀Dependency properties overview

Windows Presentation Foundation (WPF) 提供一組可用來擴充類型的屬性功能之服務。Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a type's property. 整體而言,這些服務通常稱為 WPF 屬性系統。Collectively, these services are typically referred to as the WPF property system. 受到 WPF 屬性系統支援的屬性則稱為相依性屬性。A property that is backed by the WPF property system is known as a dependency property. 本概觀描述 WPF 屬性系統和相依性屬性的功能。This overview describes the WPF property system and the capabilities of a dependency property. 這包括如何在 XAML 和程式碼中使用現有的相依性屬性。This includes how to use existing dependency properties in XAML and in code. 本概觀也介紹相依性屬性的特製化層面,例如相依性屬性中繼資料,以及如何在自訂類別中建立您自己的相依性屬性。This overview also introduces specialized aspects of dependency properties, such as dependency property metadata, and how to create your own dependency property in a custom class.

PrerequisitesPrerequisites

本主題假設您對 NET 類型系統和物件導向程式設計有基本的認識。This topic assumes that you have some basic knowledge of the .NET type system and object-oriented programming. 為了遵循本主題中的範例,您也應該了解 XAML 並知道如何撰寫 WPF 應用程式。In order to follow the examples in this topic, you should also understand XAML and know how to write WPF applications. 如需詳細資訊,請參閱逐步解說︰我的第一個 WPF 桌面應用程式For more information, see Walkthrough: My first WPF desktop application.

相依性屬性與 CLR 屬性Dependency properties and CLR properties

在 WPF 中,屬性通常會公開為標準 .NET 屬性In WPF, properties are typically exposed as standard .NET properties. 在基本層級,您可以和這些屬性直接互動,永遠不知道它們會實作為相依性屬性。At a basic level, you could interact with these properties directly and never know that they are implemented as a dependency property. 但您應該要熟悉 WPF 屬性系統的部分或全部功能,以便可以利用這些功能。However, you should become familiar with some or all of the features of the WPF property system, so that you can take advantage of these features.

相依性屬性的目的是提供一個方式,根據其他輸入的值來計算屬性值。The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs. 這些其他輸入可能包含系統屬性 (例如佈景主題和使用者喜好設定)、Just-In-Time 屬性決策機制 (例如資料繫結和動畫/腳本)、多用途的範本 (例如資源和樣式),或者透過父子關聯性與項目樹狀結構中的其他項目知道的值。These other inputs might include system properties such as themes and user preference, just-in-time property determination mechanisms such as data binding and animations/storyboards, multiple-use templates such as resources and styles, or values known through parent-child relationships with other elements in the element tree. 此外,您也可以實作相依性屬性,提供獨立的驗證、預設值、監視其他屬性變更的回撥,以及可根據潛在執行階段資訊強制轉型屬性值的系統。In addition, a dependency property can be implemented to provide self-contained validation, default values, callbacks that monitor changes to other properties, and a system that can coerce property values based on potentially runtime information. 衍生的類別也可以透過覆寫相依性屬性中繼資料,而不是覆寫現有屬性的實際實作或建立新屬性,來變更現有屬性的某些特定特性。Derived classes can also change some specific characteristics of an existing property by overriding dependency property metadata, rather than overriding the actual implementation of existing properties or creating new properties.

在 SDK 參考中,您可以在該屬性 Managed 參考頁面出現相依性屬性資訊區段時,識別哪個屬性是相依性屬性。In the SDK reference, you can identify which property is a dependency property by the presence of the Dependency Property Information section on the managed reference page for that property. 相依性屬性資訊區段包含該相依性屬性的 DependencyProperty 識別碼欄位連結,也包含為該屬性、依類別層級覆寫資訊及其他詳細資料設定的中繼資料選項清單。The Dependency Property Information section includes a link to the DependencyProperty identifier field for that dependency property, and also includes a list of the metadata options that are set for that property, per-class override information, and other details.

相依性屬性支援 CLR 屬性Dependency properties back CLR properties

相依性屬性和 WPF 屬性系統透過提供支援屬性的類型來擴充屬性功能,作為支援有私用欄位屬性之標準模式的替代實作。Dependency properties and the WPF property system extend property functionality by providing a type that backs a property, as an alternative implementation to the standard pattern of backing the property with a private field. 這個類型的名稱為 DependencyPropertyThe name of this type is DependencyProperty. 另一個定義 WPF 屬性系統的重要類型為 DependencyObjectThe other important type that defines the WPF property system is DependencyObject. DependencyObject 定義可以登錄並擁有相依性屬性的基底類別。DependencyObject defines the base class that can register and own a dependency property.

以下列出與相依性屬性搭配使用的術語:The following lists the terminology that is used with dependency properties:

  • 相依性屬性:DependencyProperty 支援的屬性。Dependency property: A property that is backed by a DependencyProperty.

  • 相依性屬性識別碼︰ 是一個 DependencyProperty 執行個體,在登錄相依性屬性時當成傳回值取得,然後儲存為類別的靜態成員。Dependency property identifier: A DependencyProperty instance, which is obtained as a return value when registering a dependency property, and then stored as a static member of a class. 此識別碼用為與 WPF 屬性系統互動的多個 API 參數。This identifier is used as a parameter for many of the APIs that interact with the WPF property system.

  • CLR「包裝函式」: 實際取得及設定屬性的實作。CLR "wrapper": The actual get and set implementations for the property. 這些實作透過在 GetValueSetValue 呼叫中使用相依性屬性識別碼,為使用 WPF 屬性系統的屬性提供支援。These implementations incorporate the dependency property identifier by using it in the GetValue and SetValue calls, thus providing the backing for the property using the WPF property system.

下例定義 IsSpinning 相依性屬性,並向其支援的屬性顯示 DependencyProperty 識別碼關聯性。The following example defines the IsSpinning dependency property, and shows the relationship of the DependencyProperty identifier to the property that it backs.

public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
    typeof(MyCode)
    );
public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}
Public Shared ReadOnly IsSpinningProperty As DependencyProperty =
    DependencyProperty.Register("IsSpinning",
                                GetType(Boolean),
                                GetType(MyCode))

Public Property IsSpinning() As Boolean
    Get
        Return CBool(GetValue(IsSpinningProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(IsSpinningProperty, value)
    End Set
End Property

屬性的命名慣例及其支援的 DependencyProperty 欄位很重要。The naming convention of the property and its backing DependencyProperty field is important. 欄位名稱一律是屬性名稱,後綴尾碼 PropertyThe name of the field is always the name of the property, with the suffix Property appended. 如需此慣例及其原因的詳細資訊,請參閱自訂相依性屬性For more information about this convention and the reasons for it, see Custom Dependency Properties.

設定屬性值Setting property values

您可以在程式碼或 XAML 中設定屬性。You can set properties either in code or in XAML.

在 XAML 中設定屬性值Setting property values in XAML

下列 XAML 範例將按鈕的背景色彩指定為紅色。The following XAML example specifies the background color of a button as red. 本範例示範 XAML 屬性的簡單字串值,它的類型由 WPF XAML 剖析器在產生的程式碼中轉換成 WPF 類型 (Color,透過 SolidColorBrush)。This example illustrates a case where the simple string value for a XAML attribute is type-converted by the WPF XAML parser into a WPF type (a Color, by way of a SolidColorBrush) in the generated code.

<Button Background="Red" Content="Button!"/>

XAML 支援各種設定屬性的語法形式。XAML supports a variety of syntax forms for setting properties. 特定屬性要使用哪種語法,取決於屬性使用的實值型別以及其他因素,例如有無類型轉換器。Which syntax to use for a particular property will depend on the value type that a property uses, as well as other factors such as the presence of a type converter. 如需屬性設定之 XAML 語法的詳細資訊,請參閱 XAML 概觀 (WPF)XAML 語法詳細資料For more information on XAML syntax for property setting, see XAML Overview (WPF) and XAML Syntax In Detail.

下列 XAML 範例為非屬性語法的範例,示範另一個按鈕的背景。As an example of non-attribute syntax, the following XAML example shows another button background. 這一次不是設定簡單的純色,而是將背景設定為映像,將表示該映像和該映像來源的項目指定為巢狀項目的屬性。This time rather than setting a simple solid color, the background is set to an image, with an element representing that image and the source of that image specified as an attribute of the nested element. 這是屬性項目語法的範例。This is an example of property element syntax.

<Button Content="Button!">
  <Button.Background>
    <ImageBrush ImageSource="wavy.jpg"/>
  </Button.Background>
</Button>

在程式碼中設定屬性Setting properties in code

在程式碼中設定相依性屬性值,通常只是呼叫 CLR "包裝函式所公開的集合實。Setting dependency property values in code is typically just a call to the set implementation exposed by the CLR "wrapper".

Button myButton = new Button();
myButton.Width = 200.0;
Dim myButton As New Button()
myButton.Width = 200.0

取得屬性值,基本上也就是呼叫 get「包裝函式」實作︰Getting a property value is also essentially a call to the get "wrapper" implementation:

double whatWidth;
whatWidth = myButton.Width;
Dim whatWidth As Double
whatWidth = myButton.Width

您也可以 GetValue 呼叫屬性系統 Api,並直接 SetValueYou can also call the property system APIs GetValue and SetValue directly. 如果您使用現有的屬性(包裝函式更方便,並為開發人員工具提供更好的屬性公開),則通常不需要這麼做,但在某些情況下,直接呼叫 Api 是適當的。This is not typically necessary if you are using existing properties (the wrappers are more convenient, and provide better exposure of the property for developer tools), but calling the APIs directly is appropriate for certain scenarios.

屬性也可以在 XAML 中設定,稍後再於程式碼中透過程式碼後置存取。Properties can be also set in XAML and then accessed later in code, through code-behind. 如需詳細資訊,請參閱 WPF 中的程式碼後置和 XAMLFor details, see Code-Behind and XAML in WPF.

相依性屬性提供的屬性功能Property functionality provided by a dependency property

相對於欄位支援屬性,相依性屬性提供能擴充屬性功能的功能。A dependency property provides functionality that extends the functionality of a property as opposed to a property that is backed by a field. 通常,這類功能代表或支援下列特定功能其中之一:Often, such functionality represents or supports one of the following specific features:

資源Resources

您可以參考資源來設定相依性屬性值。A dependency property value can be set by referencing a resource. 資源通常指定為 Resources 頁面根項目或應用程式的屬性值 (這些位置最方便存取資源)。Resources are typically specified as the Resources property value of a page root element, or of the application (these locations enable the most convenient access to the resource). 下例示範如何定義 SolidColorBrush 資源。The following example shows how to define a SolidColorBrush resource.

<DockPanel.Resources>
  <SolidColorBrush x:Key="MyBrush" Color="Gold"/>
</DockPanel.Resources>

定義資源之後,您即可參考資源並用它提供屬性值︰Once the resource is defined, you can reference the resource and use it to provide a property value:

<Button Background="{DynamicResource MyBrush}" Content="I am gold" />

這個特定的資源稱之為 DynamicResource 標記延伸 (在 WPF XAML 中,您可以使用靜態或動態資源參考)。This particular resource is referenced as a DynamicResource Markup Extension (in WPF XAML, you can use either a static or dynamic resource reference). 若要使用動態資源參考,您必須設定相依性屬性,因此它是由 WPF 屬性系統啟用的專門動態資源參考使用。To use a dynamic resource reference, you must be setting to a dependency property, so it is specifically the dynamic resource reference usage that is enabled by the WPF property system. 如需詳細資訊,請參閱 XAML 資源For more information, see XAML Resources.

注意

資源視為區域數值,這表示如果您設定另一個區域數值,就會排除資源參考。Resources are treated as a local value, which means that if you set another local value, you will eliminate the resource reference. 如需詳細資訊,請參閱相依性屬性值優先順序For more information, see Dependency Property Value Precedence.

資料繫結Data binding

相依性屬性可以透過資料繫結參考值。A dependency property can reference a value through data binding. 資料繫結的運作是透過 XAML 中的特定標記延伸語法,或程式碼中的 Binding 物件。Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. 使用資料繫結,最後一個屬性值決定會延後到執行階段,此時已自資料來源取得值。With data binding, the final property value determination is deferred until run time, at which time the value is obtained from a data source.

下列範例會使用在 XAML 中宣告的繫結來設定 ButtonContent 屬性。The following example sets the Content property for a Button, using a binding declared in XAML. 繫結使用繼承的資料內容和 XmlDataProvider 資料來源 (未顯示)。The binding uses an inherited data context and an XmlDataProvider data source (not shown). 繫結本身會指定資料來源內 XPath 所需的來源屬性。The binding itself specifies the desired source property by XPath within the data source.

<Button Content="{Binding XPath=Team/@TeamName}"/>

注意

繫結視為區域數值,這表示如果您設定另一個區域數值,就會排除繫結。Bindings are treated as a local value, which means that if you set another local value, you will eliminate the binding. 如需詳細資訊,請參閱相依性屬性值優先順序For details, see Dependency Property Value Precedence.

為了產生資料繫結作業的 DependencyObject 來源屬性值中變更之通知,相依性屬性或 DependencyObject 類別並不原生支援 INotifyPropertyChangedDependency properties, or the DependencyObject class, do not natively support INotifyPropertyChanged for purposes of producing notifications of changes in DependencyObject source property value for data binding operations. 如需如何建立資料繫結所用屬性的詳細資訊,該資料繫結可以報告資料繫結目標的變更,請參閱資料繫結概觀For more information on how to create properties for use in data binding that can report changes to a data binding target, see Data Binding Overview.

樣式Styles

樣式和範本是使用相依性屬性的兩大激勵案例。Styles and templates are two of the chief motivating scenarios for using dependency properties. 樣式特別適用於設定定義應用程式 使用者介面 (UI)user interface (UI) 的屬性。Styles are particularly useful for setting properties that define application 使用者介面 (UI)user interface (UI). 樣式通常會定義為 XAML 中的資源。Styles are typically defined as resources in XAML. 樣式與屬性系統互動,因為它們通常包含特定屬性的 "setter",以及根據另一個屬性的即時值變更屬性值的「觸發程序」。Styles interact with the property system because they typically contain "setters" for particular properties, as well as "triggers" that change a property value based on the real-time value for another property.

下列範例會建立非常簡單的樣式 (該樣式將在 Resources 字典內部中定義,未顯示),然後將該樣式直接套用於 ButtonStyle 屬性。The following example creates a very simple style (which would be defined inside a Resources dictionary, not shown), then applies that style directly to the Style property for a Button. 樣式中的 setter 會將樣式 ButtonBackground 屬性設定為綠色。The setter within the style sets the Background property for a styled Button to green.

<Style x:Key="GreenButtonStyle">
  <Setter Property="Control.Background" Value="Green"/>
</Style>
<Button Style="{StaticResource GreenButtonStyle}">I am green!</Button>

如需詳細資訊,請參閱 設定樣式和範本For more information, see Styling and Templating.

AnimationsAnimations

相依性屬性可以動畫方式顯示。Dependency properties can be animated. 套用並執行動畫時,動畫顯示值的運作優先於屬性可能執行的任何值 (例如本機值)。When an animation is applied and is running, the animated value operates at a higher precedence than any value (such as a local value) that the property otherwise has.

下列範例在 Button 屬性上以動畫方式顯示 Background (技術上來說,Background 透過使用屬性項目語法來指定空白 SolidColorBrushBackground,然後該 SolidColorBrushColor 屬性是直接繪製的屬性)。The following example animates the Background on a Button property (technically, the Background is animated by using property element syntax to specify a blank SolidColorBrush as the Background, then the Color property of that SolidColorBrush is the property that is directly animated).

<Button>I am animated
  <Button.Background>
    <SolidColorBrush x:Name="AnimBrush"/>
  </Button.Background>
  <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation
            Storyboard.TargetName="AnimBrush" 
            Storyboard.TargetProperty="(SolidColorBrush.Color)"
            From="Red" To="Green" Duration="0:0:5" 
            AutoReverse="True" RepeatBehavior="Forever" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Button.Triggers>
</Button>

如需可動畫顯示屬性的詳細資訊,請參閱動畫概觀腳本概觀For more information on animating properties, see Animation Overview and Storyboards Overview.

中繼資料覆寫Metadata overrides

當您衍生自最初登錄相依性屬性的類別時,您可以覆寫該屬性的中繼資料,變更相依性屬性的特定行為。You can change certain behaviors of a dependency property by overriding the metadata for that property when you derive from the class that originally registers the dependency property. 覆寫依賴於 DependencyProperty 識別碼的中繼資料。Overriding metadata relies on the DependencyProperty identifier. 覆寫中繼資料不需要重新實作屬性。Overriding metadata does not require re-implementing the property. 屬性系統會以原生方式處理中繼資料變更。每個類別都可能依每個類型,保存繼承自基底類別之所有屬性的個別中繼資料。The metadata change is handled natively by the property system; each class potentially holds individual metadata for all properties that are inherited from base classes, on a per-type basis.

下列範例會覆寫相依性屬性的中繼資料 DefaultStyleKeyThe following example overrides metadata for a dependency property DefaultStyleKey. 覆寫此特定相依性屬性中繼資料,是建立可使用佈景主題預設樣式控制項之實作模式的一部分。Overriding this particular dependency property metadata is part of an implementation pattern that creates controls that can use default styles from themes.

public class SpinnerControl : ItemsControl
{
    static SpinnerControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(SpinnerControl), 
            new FrameworkPropertyMetadata(typeof(SpinnerControl))
        );
    }
}
Public Class SpinnerControl
    Inherits ItemsControl
    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(SpinnerControl), New FrameworkPropertyMetadata(GetType(SpinnerControl)))
    End Sub
End Class

如需覆寫或取得屬性中繼資料的詳細資訊,請參閱相依性屬性中繼資料For more information about overriding or obtaining property metadata, see Dependency Property Metadata.

屬性值繼承Property value inheritance

項目可以繼承物件樹狀結構中的父代相依性屬性值。An element can inherit the value of a dependency property from its parent in the object tree.

注意

並非所有的相依性屬性都會啟用屬性值繼承行為,因為繼承的計算時間會影響效能。Property value inheritance behavior is not globally enabled for all dependency properties, because the calculation time for inheritance does have some performance impact. 通常只有建議屬性值繼承為適當的特定案例屬性才會啟用屬性值繼。Property value inheritance is typically only enabled for properties where a particular scenario suggests that property value inheritance is appropriate. 您可以在<SDK 參考>的相依性屬性資訊一節中查看相依性屬性,判斷該相依性屬性是否有繼承行為。You can determine whether a dependency property inherits by looking at the Dependency Property Information section for that dependency property in the SDK reference.

下例會示範繫結並設定指定繫結來源的 DataContext 屬性,之前的繫結範例未示範。The following example shows a binding, and sets the DataContext property that specifies the source of the binding, which was not shown in the earlier binding example. 子物件中的任何後續繫結都不需要指定來源,即可使用父系 StackPanel 物件中來自 DataContext 的繼承值。Any subsequent bindings in child objects do not need to specify the source, they can use the inherited value from DataContext in the parent StackPanel object. (或者,子物件可以改為選擇直接在 Binding 中指定自己的 DataContextSource,並故意不使用其繫結的資料內容之繼承值。)(Alternatively, a child object could instead choose to directly specify its own DataContext or a Source in the Binding, and to deliberately not use the inherited value for data context of its bindings.)

<StackPanel Canvas.Top="50" DataContext="{Binding Source={StaticResource XmlTeamsSource}}">
  <Button Content="{Binding XPath=Team/@TeamName}"/>
</StackPanel>

如需詳細資訊,請參閱屬性值繼承For more information, see Property Value Inheritance.

WPF 設計工具整合WPF designer integration

具有實作為相依性屬性之屬性的自訂控制項,將會收到適用于 Visual Studio 支援的適當 WPF 設計工具。A custom control with properties that are implemented as dependency properties will receive appropriate WPF Designer for Visual Studio support. 其中一例就是能夠使用 [屬性] 視窗編輯直接和附加的相依性屬性。One example is the ability to edit direct and attached dependency properties with the Properties window. 如需詳細資訊,請參閱控制項撰寫概觀For more information, see Control Authoring Overview.

相依性屬性值優先順序Dependency property value precedence

當您取得相依性屬性的值時,您就可能取得一個值,此值原透過參與 WPF 屬性系統的任何其他屬性型輸入來設定在該屬性上。When you get the value of a dependency property, you are potentially obtaining a value that was set on that property through any one of the other property-based inputs that participate in the WPF property system. 相依性屬性值有優先順序,所以屬性如何取得其值的各種案例才能以可預測的方式互動。Dependency property value precedence exists so that a variety of scenarios for how properties obtain their values can interact in a predictable way.

請參考下列範例。Consider the following example. 此範例包含適用於所有按鈕及其 Background 屬性的樣式,但也會指定一個具有本機設定 Background 值的按鈕。The example includes a style that applies to all buttons and their Background properties, but then also specifies one button with a locally set Background value.

注意

SDK 文件在討論相依性屬性時,偶爾會使用詞彙「區域數值」或「本機設定值」。The SDK documentation uses the terms "local value" or "locally set value" occasionally when discussing dependency properties. 本機設定值是一個屬性值,其直接設定在程式碼的物件執行個體上,或作為 XAML 項目上的屬性。A locally set value is a property value that is set directly on an object instance in code, or as an attribute on an element in XAML.

基本上,第一個按鈕會設定兩次屬性,但只套用一個值:有最高優先順序的值。In principle, for the first button, the property is set twice, but only one value applies: the value with the highest precedence. 本機設定值有最高優先順序 (執行中的動畫除外,但本例中未套用任何動畫),因此第一個按鈕的背景使用本機設定值,而不使用樣式 setter 值。A locally set value has the highest precedence (except for a running animation, but no animation applies in this example) and thus the locally set value is used instead of the style setter value for the background on the first button. 第二個按鈕沒有區域數值 (樣式 setter 有最高的優先順序),因此該按鈕的背景來自樣式 setter。The second button has no local value (and no other value with higher precedence than a style setter) and thus the background in that button comes from the style setter.

<StackPanel>
  <StackPanel.Resources>
    <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
     <Setter Property="Background" Value="Red"/>
    </Style>
  </StackPanel.Resources>
  <Button Background="Green">I am NOT red!</Button>
  <Button>I am styled red</Button>
</StackPanel>

為什麼會有相依性屬性優先順序?Why does dependency property precedence exist?

通常您不會希望樣式一律套用樣式,甚至隱藏個別項目的本機設定值 (否則一般很難使用樣式或項目)。Typically, you would not want styles to always apply and to obscure even a locally set value of an individual element (otherwise, it would be very difficult to use either styles or elements in general). 因此,來自樣式的值運作優先順序比本機設定值低。Therefore, the values that come from styles operate at a lower precedent than a locally set value. 如需更完整的相依性屬性清單及相依性屬性有效值的可能來源,請參閱相依性屬性值優先順序For a more thorough listing of dependency properties and where a dependency property effective value might come from, see Dependency Property Value Precedence.

注意

許多屬性是定義在相依性屬性的 WPF 項目上。There are a number of properties defined on WPF elements that are not dependency properties. 大體上,屬性過去只有在需要支援至少一個屬性系統啟用的案例時,才會實作為相依性屬性︰資料繫結、樣式、動畫、預設值支援、繼承、附加屬性或失效。By and large, properties were implemented as dependency properties only when there were needs to support at least one of the scenarios enabled by the property system: data binding, styling, animation, default value support, inheritance, attached properties, or invalidation.

深入了解相依性屬性Learning more about dependency properties

  • 附加屬性是支援 XAML 特殊語法的屬性類型。An attached property is a type of property that supports a specialized syntax in XAML. 附加屬性通常沒有與 common language runtime (CLR)屬性的1:1 對應,而且不一定是相依性屬性。An attached property often does not have a 1:1 correspondence with a common language runtime (CLR) property, and is not necessarily a dependency property. 附加屬性的一般用途是允許子項目向父項目回報屬性值,即使父項目和子項目未同時擁有列為類別成員的該屬性。The typical purpose of a attached property is to allow child elements to report property values to a parent element, even if the parent element and child element do not both possess that property as part of the class members listings. 一個主要案例是使子項目能夠告知父系應如何在 UIUI 中將其呈現;如需範例,請參閱 DockLeftOne primary scenario is to enable child elements to inform the parent how they should be presented in UIUI; for an example, see Dock or Left. 如需詳細資訊,請參閱附加屬性概觀For details, see Attached Properties Overview.

  • 元件開發人員或應用程式開發人員可能希望建立自己的相依性屬性,以便啟用資料繫結或樣式支援等功能,或用於失效和值的強制型轉支援。Component developers or application developers may wish to create their own dependency property, in order to enable capabilities such as data binding or styles support, or for invalidation and value coercion support. 如需詳細資訊,請參閱自訂相依性屬性For details, see Custom Dependency Properties.

  • 相依性屬性通常應該視為公用屬性,任何可存取執行個體的呼叫端皆可存取或至少可探索它們。Dependency properties should generally be considered to be public properties, accessible or at least discoverable by any caller that has access to an instance. 如需詳細資訊,請參閱相依性屬性的安全性For more information, see Dependency Property Security.

請參閱See also