Binding.ElementName property

Applies to Windows and Windows Phone

Gets or sets the name of the element to use as the binding source for the Binding.

Syntax


public string ElementName { get; set; }


<Binding ElementName="nameString"/>

XAML Values

nameString

The value of the Name property or x:Name attribute for the element you want to use as the binding source.

Property value

Type: System.String [.NET] | Platform::String [C++]

The value of the Name property or x:Name attribute for the element you want to use as the binding source. The default is an empty string.

Remarks

When you set this property, the specified value must refer to an element in one of the following locations:

  • The current XAML namescope.
  • The XAML namescope of the templated parent if the binding target is in a data template or control template.
Because of this restriction, you cannot use the ElementName property to bind to elements that are not created by using XAML. To bind to elements created programmatically, use the Source property instead. Source, RelativeSource, and ElementName are mutually exclusive in a binding. If you have set one of these attributes, then setting either of the other two in a binding (through XAML or through code) will cause an exception.

Bindings that use an ElementName value almost always include a simple Path that names a property that exists on the object being referenced. It's common that the source property from the named element and the destination property on the target have the same name and use the same value type, this is a scenario you see fairly often in control template definitions that composite control parts but surface properties from the parts. If the source and destination properties use different types, specify a Converter value that can resolve the type difference and return an appropriate value for the destination.

For more info about XAML namescopes and how they influence bindings and other programmatic access to objects created from XAML, see XAML namescopes.

The value of ElementName can technically be any string, but the values of Name and x:Name are enforced to be valid in the XamlName grammar, so you're typically bound by that grammar for an ElementName value too. For more info on what's valid for an element name, see the "XamlName grammar" section in the x:Name attribute reference.

You can't set the property values of a Binding object after that binding has been attached to a target element and target property. If you attempt this you'll get a run-time exception.

Examples

The following code example demonstrates how to establish a two-way binding between a TextBox and a Slider control. For the complete code listing, see scenario 1 in the XAML data binding sample.


<Slider x:Name="slider1" Minimum="1" Maximum="100"/>
<TextBox Text="{Binding ElementName=slider1, Path=Value, Mode=TwoWay}"/>


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Data
Windows::UI::Xaml::Data [C++]

Metadata

Windows.winmd

See also

Binding
XAML data binding sample
Data binding overview
XAML namescopes
x:Name attribute

 

 

Show:
© 2014 Microsoft