RelativeSource 類別

定義

實作標記延伸,這個標記延伸描述與繫結目標位置相對的繫結來源位置。

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
[<System.Windows.Markup.MarkupExtensionReturnType(typeof(System.Windows.Data.RelativeSource))>]
type RelativeSource = class
    inherit MarkupExtension
    interface ISupportInitialize
Public Class RelativeSource
Inherits MarkupExtension
Implements ISupportInitialize
繼承
RelativeSource
屬性
實作

範例

下列範例顯示建立 ToolTip 可報告驗證錯誤訊息的 樣式觸發程式。 RelativeSource使用 屬性時,setter 的值會使用樣式) 系結至目前 TextBox (TextBox 的錯誤內容。 如需此範例的詳細資訊,請參閱 如何:實作系結驗證

<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>

下列範例顯示 Style 名為 NumericUpDown 的自訂控制項定義。 TextTextBlock 屬性系結至 ValueTemplatedParent 物件之 的 ,這是 NumericUpDown 在此案例中套用這個 Style 的控制項。

<!--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>

下列傳回從系結之目標專案開始的上路徑上遇到的第二 ItemsControl 個 。

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 屬性。 這是該屬性的屬性類型。 RelativeSource 同時是標記延伸實作和資料物件。 當延伸模組傳回本身 (看到 ProvideValue) 時,相關資訊會包含在資料中。 標記延伸的主要目的是允許屬性形式的變數引數建構函式語法,讓 FindAncestor 模式可以內嵌在上階類型和其他模式不需要的兩個額外引數中。

如需 XAML 資訊,請參閱 RelativeSource MarkupExtension

建構函式

RelativeSource()

初始化 RelativeSource 類別的新執行個體。

RelativeSource(RelativeSourceMode)

以初始模式來初始化 RelativeSource 類別的新執行個體。

RelativeSource(RelativeSourceMode, Type, Int32)

以初始模式加上尋找所需相對來源的額外樹狀目錄結構導覽限定詞,初始化 RelativeSource 類別的新執行個體。

屬性

AncestorLevel

FindAncestor 模式,取得或設定要尋找的上階層級。 使用 1 來表示最接近繫結目標項目的項目。

AncestorType

取得或設定要尋找的祖系型別。

Mode

取得或設定 RelativeSourceMode 值,這個值描述與繫結目標位置相對的繫結來源位置。

PreviousData

取得靜態值,作用是傳回針對 RelativeSource 模式建構的 PreviousData

Self

取得靜態值,作用是傳回針對 RelativeSource 模式建構的 Self

TemplatedParent

取得靜態值,作用是傳回針對 RelativeSource 模式建構的 TemplatedParent

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ProvideValue(IServiceProvider)

傳回物件,此物件應該設定為目標物件對於這個標記延伸之屬性上的值。 對於 RelativeSource,這是另一個 RelativeSource,使用指定之模式的適當來源。

ShouldSerializeAncestorLevel()

指示是否應該保存 AncestorLevel 屬性。

ShouldSerializeAncestorType()

指示是否應該保存 AncestorType 屬性。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ISupportInitialize.BeginInit()

此成員支援Windows Presentation Foundation (WPF) 基礎結構,而且不適合直接從程式碼使用。

ISupportInitialize.EndInit()

此成員支援Windows Presentation Foundation (WPF) 基礎結構,而且不適合直接從程式碼使用。

適用於

另請參閱