You can't do this directly.
The
Window
and
SecondControl
are two different contexts (WPF XAML NameScope) and the
ElementName
is limited by NameScope.
"You can refer to elements in code only if they are registered to the appropriate NameScope"
MSDN: Binding.ElementName[
^]
So, I see two possibilities:
1.
If the
FirstNameTextBlock/LastNameTextBlock
controls of the
Window
are
NOT "hardcoded", but are instead data bound to the
Window
's
DataContext
, then the
SecondControl
can use the same binding descriptions, since it has inherited the
DataContext
from the
Window
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns:local="clr-namespace:WpfApplication1">
<Grid x:Name="LayoutRootGrid">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="FirstNameTextBlock" Grid.Row="0"
Text="{Binding Path=FirstName}"/>
<TextBox x:Name="LastNameTextBlock" Grid.Row="1"
Text="{Binding Path=LastName}"/>
<local:SecondControl x:Name="SecondControl" Grid.Row="2"/>
</Grid>
</Window>
<UserControl x:Class="WpfApplication1.SecondControl">
<StackPanel Background="CadetBlue">
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
Text="{Binding Path=FirstName}" />
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
Text="{Binding Path=LastName}" />
</StackPanel>
</UserControl>
2. (and probably better) would be to have the
SecondControl
contain
DependencyProperties
that can take the binding where the
SecondControl
is used in the
Window
:
<Window x:Class="WpfApplication1.MainWindow"
xmlns:local="clr-namespace:WpfApplication1">
<Grid x:Name="LayoutRootGrid">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="FirstNameTextBlock" Grid.Row="0"
Text="John"/>
<TextBox x:Name="LastNameTextBlock" Grid.Row="1"
Text="Smith"/>
<local:SecondControl x:Name="SecondControl" Grid.Row="2"
FirstName="{Binding ElementName=FirstNameTextBlock, Path=Text}"
LastName="{Binding ElementName=LastNameTextBlock, Path=Text}" />
</Grid>
</Window>
<UserControl x:Class="WpfApplication1.SecondControl" x:Name="SecondControl" >
<StackPanel Background="CadetBlue">
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
Text="{Binding ElementName=SecondControl, Path=FirstName}" />
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
Text="{Binding ElementName=SecondControl, Path=LastName}" />
</StackPanel>
</UserControl>
In
SecondControl.xaml.cs
:
public static readonly DependencyProperty FirstNameProperty =
DependencyProperty.Register("FirstName", typeof(string), typeof(SecondControl), new UIPropertyMetadata(string.Empty));
public string FirstName
{
get { return (string)GetValue(FirstNameProperty); }
set { SetValue(FirstNameProperty, value); }
}
public static readonly DependencyProperty LastNameProperty =
DependencyProperty.Register("LastName", typeof(string), typeof(SecondControl), new UIPropertyMetadata(string.Empty));
public string LastName
{
get { return (string)GetValue(LastNameProperty); }
set { SetValue(LastNameProperty, value); }
}