Gets or sets the name of the element to use as the binding source object.
Namespace:
System.Windows.Data
Assembly:
PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Visual Basic (Declaration)
Public Property ElementName As String
Dim instance As Binding
Dim value As String
value = instance.ElementName
instance.ElementName = value
public string ElementName { get; set; }
public:
property String^ ElementName {
String^ get ();
void set (String^ value);
}
public function get ElementName () : String
public function set ElementName (value : String)
<object ElementName="string" .../>
Property Value
Type:
System..::.StringThe value of the Name property or x:Name Attribute of the element of interest. You can refer to elements in code only if they are registered to the appropriate NameScope through RegisterName. For more information, see WPF XAML Namescopes.
The default is nullNothingnullptra null reference (Nothing in Visual Basic).
This property is useful when you want to bind to the property of another element in your application. 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.
By default, bindings inherit the data context specified by the DataContext property, if one has been set. 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.
The Source and RelativeSource properties of the Binding class also enable you to set the source of the binding explicitly. 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.
This example shows how to bind the property of one instantiated control to that of another using the ElementName property.
The following example shows how to bind the Background property of a Canvas to the SelectedItem.Content property of a ComboBox:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="460" Height="200"
Title="Binding the Properties of Two Controls">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<Style TargetType="Canvas">
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="50"/>
<Setter Property="Margin" Value="8"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Width" Value="150"/>
<Setter Property="Margin" Value="8"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</Window.Resources>
<Border Margin="10" BorderBrush="Silver" BorderThickness="3" Padding="8">
<DockPanel>
<TextBlock>Choose a Color:</TextBlock>
<ComboBox Name="myComboBox" SelectedIndex="0">
<ComboBoxItem>Green</ComboBoxItem>
<ComboBoxItem>Blue</ComboBoxItem>
<ComboBoxItem>Red</ComboBoxItem>
</ComboBox>
<Canvas>
<Canvas.Background>
<Binding ElementName="myComboBox" Path="SelectedItem.Content"/>
</Canvas.Background>
</Canvas>
</DockPanel>
</Border>
</Window>
When this example is rendered it looks like the following:
.png)
For the complete sample, see Binding the Properties of UI Elements Sample.
Note The binding target property (in this example, the Background property) must be a dependency property. For more information, see Data Binding Overview.
More Code
| How to: Specify the Binding Source |
In data binding, the binding source object refers to the object you obtain your data from. This topic describes the different ways of specifying the binding source.
|
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0
Reference
Other Resources