I want to refer to another element, which resides as nested under a custom user control:
<userControls:Test>
<TextBox Name="Foo" />
<Button CommandParameter="{Binding Text, ElementName=Foo" Command="{Binding anything}" />
</userControls:Test>
Basically I’m using commands and I’m trying to refer to another element with a name, but I get this famous:
Cannot set Name attribute value ‘…’ on element ‘…’. ‘…’ is under
the scope of element ‘…’, which already had a name registered when
it was defined in another scope.
I figured it’s impossible to name an element: How to create a WPF UserControl with NAMED content
So is there any other way to refer to the element that would work inside nested custom user control content?
I ended up doing it like this:
And code:
The
UIHelperis from https://stackoverflow.com/a/1759923/283055. The button is also given a name the same way as the text box.Basically all names are set via
Initializedevent. The only down side is that you must refer to the names in code, and that’s why I’m setting commands via code after the entire component has loaded. I found this the best compromise between code and UI markup.