Binding.ElementName 属性

定义

获取或设置要用作绑定源对象的元素的名称。Gets or sets the name of the element to use as the binding source object.

public:
 property System::String ^ ElementName { System::String ^ get(); void set(System::String ^ value); };
public string ElementName { get; set; }
member this.ElementName : string with get, set
Public Property ElementName As String

属性值

相关元素的 Name 属性或 x:Name Directive 的值。The value of the Name property or x:Name Directive of the element of interest. 只有通过 NameScope 将元素注册到适当的 RegisterName 后,才能在代码中引用这些元素。You can refer to elements in code only if they are registered to the appropriate NameScope through RegisterName. 有关详细信息,请参阅 WPF XAML NamescopesFor more information, see WPF XAML Namescopes.

默认值为 nullThe default is null.

注解

如果要绑定到应用程序中的另一个元素的属性,则此属性很有用。This property is useful when you want to bind to the property of another element in your application. 例如,如果想要使用 Slider 来控制应用程序中另一个控件的高度,或者要将控件的 Content 绑定到 ListBox 控件的 SelectedValue 属性。For example, if you want to use a Slider to control the height of another control in your application, or if you want to bind the Content of your control to the SelectedValue property of your ListBox control.

默认情况下,绑定将继承 DataContext 属性指定的数据上下文(如果已设置)。By default, bindings inherit the data context specified by the DataContext property, if one has been set. 不过,ElementName 属性是您显式设置 Binding 源并重写继承的数据上下文的方式之一。However, the ElementName property is one of the ways you can explicitly set the source of a Binding and override the inherited data context. 有关详细信息,请参阅如何:指定绑定源For more information, see How to: Specify the Binding Source.

使用 Binding 类的 SourceRelativeSource 属性,还可以显式设置绑定的源。The Source and RelativeSource properties of the Binding class also enable you to set the source of the binding explicitly. 但是,只应为每个绑定设置三个属性中的一个(ElementNameSourceRelativeSource),否则可能会发生冲突。However, only one of the three properties, ElementName, Source, and RelativeSource, should be set for each binding, or a conflict might occur. 如果存在绑定源冲突,则此属性将引发异常。This property throws an exception if there is a binding source conflict.

适用于