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>

下面的示例演示Style一个名NumericUpDown为的自定义控件的定义。The following example shows the Style definition of a custom control called NumericUpDown. TemplatedParent StyleText 属性绑定Value到作为的NumericUpDown对象的, 这是在这种情况下应用此的控件。 TextBlockThe 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>

有关完整示例, 请参阅NumericUpDown 自定义控件和主题和 UI 自动化支持示例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.

有关信息, 请参阅RelativeSource MarkupExtension。 XAMLXAMLFor 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()

返回一个表示当前对象的 string。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.

适用于

另请参阅