{RelativeSource} 標記延伸{RelativeSource} markup extension

提供在執行階段物件圖形中指定繫結來源的相對關係的方法。Provides a means to specify the source of a binding in terms of a relative relationship in the run-time object graph.

XAML 屬性用法 (Self 模式)XAML attribute usage (Self mode)

<Binding RelativeSource="{RelativeSource Self}" .../>
<object property="{Binding RelativeSource={RelativeSource Self} ...}" .../>

XAML 屬性用法 (TemplatedParent 模式)XAML attribute usage (TemplatedParent mode)

<Binding RelativeSource="{RelativeSource TemplatedParent}" .../>
<object property="{Binding RelativeSource={RelativeSource TemplatedParent} ...}" .../>

XAML 值XAML values

詞彙Term 描述Description
{RelativeSource Self}{RelativeSource Self} 產生的 Mode 值是 SelfProduces a Mode value of Self. 目標元素應做為這個繫結的來源。The target element should be used as the source for this binding. 在同一個元素中繫結一個元素的屬性到另一個元素時,這個值很有用。This is useful for binding one property of an element to another property on the same element.
{RelativeSource TemplatedParent}{RelativeSource TemplatedParent} 產生 ControlTemplate,以套用來做為這個繫結的來源。Produces a ControlTemplate that is applied as the source for this binding. 在範本層級將執行階段資訊套用到繫結時,這個值很有用。This is useful for applying runtime information to bindings at the template level.  


Binding 可以將 Binding.RelativeSource 設成 Binding 物件元素上的屬性,或是設成 {Binding} 標記延伸內的元件。A Binding can set Binding.RelativeSource either as an attribute on a Binding object element or as a component within a {Binding} markup extension. 這就是為什麼顯示兩個不同 XAML 語法的緣故。This is why two different XAML syntaxes are shown.

RelativeSource{Binding} 標記延伸類似。RelativeSource is similar to {Binding} markup extension. 它也是可以傳回本身的執行個體的標記延伸,並且支援實質上會傳遞引數到建構函式的字串型建構。It is a markup extension that is capable of returning instances of itself, and supporting a string-based construction that essentially passes an argument to the constructor. 在此案例中,所傳遞的引數為 Mode 值。In this case, the argument being passed is the Mode value.

將元素的一個屬性繫結到同一元素的另一個屬性時,Self 模式就很有用,且這也是不需命名然後再自我參考元素的 ElementName 繫結的變化型。The Self mode is useful for binding one property of an element to another property on the same element, and is a variation on ElementName binding but does not require naming and then self-referencing the element. 如果您將一個元素的屬性繫結到同一元素的另一個屬性,這兩個屬性必須使用相同的屬性類型,或者是您也必須在要轉換值的繫結中使用 ConverterIf you bind one property of an element to another property on the same element, either the properties must use the same property type, or you must also use a Converter on the binding to convert the values. 例如,您可以使用 Height 做為 Width 的來源而不需轉換,但是如果要使用 IsEnabled 做為 Visibility 的來源,就需要轉換器。For example, you could use Height as a source for Width without conversion, but you'd need a converter to use IsEnabled as a source for Visibility.

以下為範例。Here's an example. 這個 Rectangle 使用一個 {Binding} 標記延伸,這樣便能讓它的 HeightWidth 一律相等,且會轉譯成正方形。This Rectangle uses a {Binding} markup extension so that its Height and Width are always equal and it renders as a square. 只有 Height 是設定成固定值。Only the Height is set as a fixed value. 針對這個 Rectangle,它的預設 DataContextnull,而不是 thisFor this Rectangle its default DataContext is null, not this. 因此,若要建立資料內容來源以做為物件本身 (並啟用繫結至它的其他屬性),我們會在 {Binding} 標記延伸用法中使用 RelativeSource={RelativeSource Self} 引數。So to establish the data context source to be the object itself (and enable binding to its other properties) we use the RelativeSource={RelativeSource Self} argument in the {Binding} markup extension usage.

  Fill="Orange" Width="200"
  Height="{Binding RelativeSource={RelativeSource Self}, Path=Width}"

另一個 RelativeSource={RelativeSource Self} 的用法,是將物件的 DataContext 設定到其本身的做法。Another use of RelativeSource={RelativeSource Self} is as a way to set an object's DataContext to itself. 例如,您可能會在某些 SDK 範例中看到這項技術,其中 頁面 類別已擴充為自訂屬性,該屬性已經針對自己的資料系結提供現成的視圖模型,例如: <common:LayoutAwarePage ... DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}">For example, you may see this technique in some of the SDK examples where the Page class has been extended with a custom property that's already providing a ready-to-go view model for its own data binding such as: <common:LayoutAwarePage ... DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}">

注意   RelativeSource的 XAML 使用方式只會顯示其預期的用法:將 Xaml 中 RelativeSource 的值設定為系結運算式的一部分Note  The XAML usage for RelativeSource shows only the usage for which it is intended: setting a value for Binding.RelativeSource in XAML as part of a binding expression. 理論上,如果設定值為 RelativeSource 的屬性,也可以有其他用法。Theoretically, other usages are possible if setting a property where the value is RelativeSource.