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

I checked out the following thread and followed the example to try and bind
Does the following function cause stack overthrow eventually? var isFinish= false; function foo(){ //
Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator
Does anyone know how to change icons in Silverlight outofbrowser application?
Does anyone know if views states (used in Silverlight) will be available in next
Does C# allow hashtables to be populated in one-line expressions? I am thinking of
Does anyone have a nice solution for GalleryView when I want to display photos
Does anyone know how to add an item to a list but do it
Does map() iterate through the list like for would? Is there a value in
Does anyone know of a "similar words or keywords" algorithm available in open source

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.