Why can't I access a TextBox by name using FindName ()?

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 for:

 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 the answer:

Thanks a lot, Bruno, it worked out well. In order not to add the same name twice, I conclude it with the following:

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

Or if you register anything other than TextBoxes, the generic version:

 void RegisterControl<T>(string textBoxName, T textBox) { if ((T)this.FindName(textBoxName) != null) this.UnregisterName(textBoxName); this.RegisterName(textBoxName, textBox); } 
+6
c # wpf xaml textbox
source share
1 answer

This is due to the WPF XAML Namescopes .

Since you are adding items to the parsed item trees, you need to call RegisterName .

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

Adding elements to the analyzed element Trees

Any additions to the element tree after initial loading and processing should be called by the corresponding RegisterName implementation for the class that defines the XAML namescope. Otherwise, the added object cannot be referenced by name using methods such as FindName. The Simple Name Property (or x: Name Attribute) does not register this name in any XAML names. Adding a named element to the element tree that has a XAML namescope also does not register the name in the XAML namescope. Although XAML names may be nested, you usually register the names in the XAML namescope that exists in the root of the element, so your XAML namespace matches the XAML nickname that would be created in the equivalent loaded XAML page. Most common scenario for developers to use is that you will use RegisterName to register names in XAML names at the current root of the page. RegisterName is part of one important script for finding storyboards that will work as animations. For more information, see Storyboard Overview. If you call RegisterName on an element other than the root element in the same tree object, the name is still the registered element closest to the root, what would you call RegisterName on the root element.

+15
source share

All Articles