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

  • SEARCH
  • Home
  • 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 479733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:49:18+00:00 2026-05-13T00:49:18+00:00

Why does FindName() return null in the following example? XAML: <Window x:Class=TestDynamicTextBox343.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

  • 0

Why does FindName() return null in the following example?

XAML:

<Window x:Class="TestDynamicTextBox343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Border >

            <DockPanel x:Name="FormBase" LastChildFill="True">

            </DockPanel>

        </Border>

        <Button Content="Save" Click="Button_Click"/>
    </StackPanel>
</Window>

Code Behind:

using System;
using System.Windows;
using System.Windows.Controls;

namespace TestDynamicTextBox343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();


            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;

            TextBlock textBlock = new TextBlock();
            textBlock.Text = "First Name: ";

            TextBox textBox = new TextBox();
            textBox.Name = "FirstName";
            textBox.Text = "test";

            sp.Children.Add(textBlock);
            sp.Children.Add(textBox);
            FormBase.Children.Add(sp);

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextBox tb = (TextBox)this.FindName("FirstName");
            Console.WriteLine(tb.Text);
        }
    }
}

Addendum to Answer:

Thanks a lot, Bruno, that worked well. In order not to add the same name twice, I wrap it with this:

void RegisterTextBox(string textBoxName, TextBox textBox)
{
    if ((TextBox)this.FindName(textBoxName) != null)
        this.UnregisterName(textBoxName);
    this.RegisterName(textBoxName, textBox);
}

Or if you will be registering anything other than TextBoxes, a generic version:

void RegisterControl<T>(string textBoxName, T textBox)
{
    if ((T)this.FindName(textBoxName) != null)
        this.UnregisterName(textBoxName);
    this.RegisterName(textBoxName, textBox);
}
  • 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-05-13T00:49:18+00:00Added an answer on May 13, 2026 at 12:49 am

    This is related to WPF XAML Namescopes.

    Because you add elements to parsed element trees, you need to call RegisterName.

            ...
            TextBox textBox = new TextBox();
            textBox.Name = "FirstName";
            textBox.Text = "test";
    
            this.RegisterName("FirstName", textBox);
            ...
    

    Adding Elements to Parsed Element
    Trees

    Any additions to the element tree
    after initial loading and processing
    must call the appropriate
    implementation of RegisterName for the
    class that defines the XAML namescope.
    Otherwise, the added object cannot be
    referenced by name through methods
    such as FindName. Merely setting a
    Name property (or x:Name Attribute)
    does not register that name into any
    XAML namescope. Adding a named element
    to an element tree that has a XAML
    namescope also does not register the
    name to the XAML namescope. Although
    XAML namescopes can be nested, you
    generally register names to the XAML
    namescope that exists on the root
    element, so that your XAML namescope
    location parallels the XAML namescope
    that would have been created in an
    equivalent loaded XAML page. The most
    common scenario for application
    developers is that you will use
    RegisterName to register names into
    the XAML namescope on the current root
    of the page. RegisterName is part of
    one important scenario for finding
    storyboards that will run as
    animations. For more information, see
    Storyboards Overview. If you call
    RegisterName on an element other than
    the root element in the same object
    tree, the name is still registered to
    the element nearest the root, as if
    you had called RegisterName on the
    root element.

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

Sidebar

Related Questions

Does anyone know why UsernameExists wont return True. I must have my syntax messed
Does anyone out there know how to integrate ContentFlow ( http://www.jacksasylum.eu/ContentFlow ) and Lightbox2
Does the following psuedo code accomplish my goal of cleaning up after myself when
I checked out the following thread and followed the example to try and bind
Does anyone have an example of a stored procedure which makes a connection to
Does the Swing API have classes which combine InputVerifiers? For example, I have three
Does anyone have any experience with PHP QuickHash ( http://php.net/manual/en/book.quickhash.php )? Some early testing
I have the following set: class Element (var Name:String, var Description: String) var MoreElement:
Does anyone know a more efficient way to write the following. my load time
Does D support template template parameters? How would I get the following to work?

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.