In my WPF Window i have button with command
<Button.CommandParameter>
<MultiBinding Converter="{converters:Converter_MultipleCommandParameters}">
<Binding ElementName="passwordBoxSQL" />
<Binding ElementName="passwordBoxR2" />
</MultiBinding>
</Button.CommandParameter>
and passwordbox with x:Name
<PasswordBox
x:Name="passwordBoxSQL"
Grid.Row="4"
Grid.Column="1"
Margin="0,0,10,0"
VerticalAlignment="Center" />
It works well.
Then I moved Grid with passwordbox to another UserControl named Test in namespace XYZ.Control. In WPF windows i use
<Window
xmlns:local="clr-namespace:XYZ.Control"
....
<local:Test />
After running the program, everything can be seen well, but there is an error:
XAML Binding Failures Description - Cannot find source: ElementName=passwordBoxSQL.
What should I give in Binding ElementName? Maybe it needs to be done somehow differently?
I warn you right away that I agree with @Clemens that it is incorrect to search for an element by name inside a child UserControl. But for educational purposes I will show you a solution based on throwing the element name into the Window visibility area.
public partial class TestUserControl1 : UserControl
{
public TestUserControl1()
{
Loaded += OnLoaded;
Unloaded += OnUnloaded;
InitializeComponent();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window.UnregisterName(nameof(passwordBoxSQL));
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window?.RegisterName(nameof(passwordBoxSQL), passwordBoxSQL);
}
}