RelativeSource 類別

定義

實作標記延伸,這個標記延伸描述與繫結目標位置相對的繫結來源位置。Implements a markup extension that describes the location of the binding source relative to the position of the binding target.

public ref class RelativeSource : System::Windows::Markup::MarkupExtension, System::ComponentModel::ISupportInitialize
[System.Windows.Markup.MarkupExtensionReturnType(typeof(System.Windows.Data.RelativeSource))]
public class RelativeSource : System.Windows.Markup.MarkupExtension, System.ComponentModel.ISupportInitialize
type RelativeSource = class
    inherit MarkupExtension
    interface ISupportInitialize
Public Class RelativeSource
Inherits MarkupExtension
Implements ISupportInitialize
繼承
RelativeSource
屬性
實作

範例

下列範例顯示的樣式觸發程式會建立ToolTip報告驗證錯誤訊息的。The following example shows a style trigger that creates a ToolTip that reports a validation error message. 使用屬性時, setter 的值會系結至目前TextBox的錯誤內容 ( TextBox使用樣式)。 RelativeSourceUsing the RelativeSource property, the value of the setter binds to the error content of the current TextBox (the TextBox using the style). 如需此範例的詳細資訊, 請參閱如何:執行系結驗證。For more information on this example, see How to: Implement Binding Validation.

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)/ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

下列範例會顯示StyleNumericUpDown為的自訂控制項定義。The following example shows the Style definition of a custom control called NumericUpDown. Value StyleText屬性TextBlock會系結至TemplatedParent物件的NumericUpDown , 也就是這種情況下套用的控制項。The Text property of the TextBlock is bound to the Value of the object that is the TemplatedParent, which is the NumericUpDown control that this Style is applied to in this case.

<!--ControlTemplate for NumericUpDown that inherits from
    Control.-->
<Style TargetType="{x:Type local:NumericUpDown}">
  <Setter Property="HorizontalAlignment" Value="Center"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
        <Grid Margin="3">
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>

          <Border BorderThickness="1" BorderBrush="Gray" 
                  Margin="2" Grid.RowSpan="2" 
                  VerticalAlignment="Center" HorizontalAlignment="Stretch">
                  
            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" 
                       Width="60" TextAlignment="Right" Padding="5"/>
          </Border>
          
          <RepeatButton Command="{x:Static local:NumericUpDown.IncreaseCommand}"  
                        Grid.Column="1" Grid.Row="0">Up</RepeatButton>
                        
          <RepeatButton Command="{x:Static local:NumericUpDown.DecreaseCommand}"
                        Grid.Column="1" Grid.Row="1">Down</RepeatButton>

        </Grid>

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

如需完整範例, 請參閱使用主題和 UI 自動化支援範例來 NumericUpDown 自訂控制項For the complete sample, see NumericUpDown Custom Control with Theme and UI Automation Support Sample.

下列會傳回從系ItemsControl結的目標元素開始, 在向上路徑上遇到的第二個。The following returns the second ItemsControl encountered on the upward path starting at the target element of the binding.

Binding myBinding = new Binding();
// Returns the second ItemsControl encountered on the upward path
// starting at the target element of the binding
myBinding.RelativeSource = new RelativeSource(
    RelativeSourceMode.FindAncestor, typeof(ItemsControl), 2);
Dim myBinding As New Binding()
' Returns the second ItemsControl encountered on the upward path
' starting at the target element of the binding
myBinding.RelativeSource = New RelativeSource(RelativeSourceMode.FindAncestor, GetType(ItemsControl), 2)

備註

指定系結來源的其中一個方法是使用Binding.RelativeSource屬性。One of the ways to specify a binding source is to use the Binding.RelativeSource property. 這是該屬性的屬性型別。This is the property type of that property. RelativeSource同時為標記延伸模組的執行和資料物件。RelativeSource is simultaneously a markup extension implementation and a data object. 當擴充功能傳回本身時 ( ProvideValue請參閱), 相關資訊會包含在資料中。When the extension returns itself (see ProvideValue), the relevant information is contained in the data. 標記延伸的主要目的是要允許在屬性形式中使用可變引數的函式語法, 如此FindAncestor一來, 就可以針對其他模式不需要的上階型別和層級, 以內嵌的兩個額外引數來定義模式。The main purpose of the markup extension is to allow a variable-argument constructor syntax in attribute form, so that the FindAncestor mode can be defined inline with the two extra arguments for ancestor type and level that the other modes do not require.

XAMLXAML需相關資訊, 請參閱RelativeSource MarkupExtensionFor XAMLXAML information, see RelativeSource MarkupExtension.

建構函式

RelativeSource()

初始化 RelativeSource 類別的新執行個體。Initializes a new instance of the RelativeSource class.

RelativeSource(RelativeSourceMode)

以初始模式來初始化 RelativeSource 類別的新執行個體。Initializes a new instance of the RelativeSource class with an initial mode.

RelativeSource(RelativeSourceMode, Type, Int32)

以初始模式加上尋找所需相對來源的額外樹狀目錄結構導覽限定詞,初始化 RelativeSource 類別的新執行個體。Initializes a new instance of the RelativeSource class with an initial mode and additional tree-walking qualifiers for finding the desired relative source.

屬性

AncestorLevel

FindAncestor 模式,取得或設定要尋找的上階層級。Gets or sets the level of ancestor to look for, in FindAncestor mode. 使用 1 來表示最接近繫結目標項目的項目。Use 1 to indicate the one nearest to the binding target element.

AncestorType

取得或設定要尋找的祖系型別。Gets or sets the type of ancestor to look for.

Mode

取得或設定 RelativeSourceMode 值,這個值描述與繫結目標位置相對的繫結來源位置。Gets or sets a RelativeSourceMode value that describes the location of the binding source relative to the position of the binding target.

PreviousData

取得靜態值,作用是傳回針對 RelativeSource 模式建構的 PreviousDataGets a static value that is used to return a RelativeSource constructed for the PreviousData mode.

Self

取得靜態值,作用是傳回針對 RelativeSource 模式建構的 SelfGets a static value that is used to return a RelativeSource constructed for the Self mode.

TemplatedParent

取得靜態值,作用是傳回針對 RelativeSource 模式建構的 TemplatedParentGets a static value that is used to return a RelativeSource constructed for the TemplatedParent mode.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ProvideValue(IServiceProvider)

傳回物件,此物件應該設定為目標物件對於這個標記延伸之屬性上的值。Returns an object that should be set as the value on the target object's property for this markup extension. 對於 RelativeSource,這是另一個 RelativeSource,使用指定之模式的適當來源。For RelativeSource, this is another RelativeSource, using the appropriate source for the specified mode.

ShouldSerializeAncestorLevel()

指示是否應該保存 AncestorLevel 屬性。Indicates whether the AncestorLevel property should be persisted.

ShouldSerializeAncestorType()

指示是否應該保存 AncestorType 屬性。Indicates whether the AncestorType property should be persisted.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ISupportInitialize.BeginInit()

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但是您不可以從程式碼直接使用它。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

ISupportInitialize.EndInit()

這個成員支援 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基礎結構,但是您不可以從程式碼直接使用它。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

適用於

另請參閱