Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:29:22+00:00 2026-06-07T14:29:22+00:00

I want to refer to another element, which resides as nested under a custom

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:29:25+00:00Added an answer on June 7, 2026 at 2:29 pm

    I ended up doing it like this:

    <UserControl x:Class="MyProject.FooBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:userControls="clr-namespace:MyProject.UserControls"
             Loaded="Loaded"> <!-- notice -->
    
        <userControls:Test>
             <TextBox Initialized="FooInitialized" />
    
             <Button Initialized="AnotherInitialized" />
        </userControls:Test>
    

    And code:

    // This for every initialization, all we do is set names after the elements are initialized.
    private void FooInitialized(object sender, EventArgs e)
    {
        ((TextBox) sender).Name = "Foo";
    }
    
    // Here when the entire junk is loaded, we set the necessary commands.
    private void Loaded(object sender, EventArgs e)
    {
        // Find elements.
        var button = UIHelper.FindChild<Button>(this, "Bar");
        var textBox = UIHelper.FindChild<TextBox>(this, "Foo");
    
        // Set up bindings for button.
        button.Command = ((FooViewModel) DataContext).FooCommand;
        button.CommandParameter = textBox;
    }
    

    The UIHelper is 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 Initialized event. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to reference another property in java.util.Properties? I want to refer to
I have in a JFrame some components that I want to refer into another
I want to refer to the value of an empty text field because I
I want to refer to a static Java variable in my styles.xml, is that
I want to refer to this post, because it might relate: Make Form Fields
When I create SQL tables and I want to refer to a membership provider
I have an instance of Class A that I want to refer to in
Python beginner here, I have a list of lists and want to refer to
How can i refer to the object i use inside With if i want
I want to use rsync to synchronize two directories in both directions. I refer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.