Click here to Skip to main content
15,886,100 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void Button1_Click(object sender, RoutedEventArgs e)
{
    TextBlock Var_TextBox = new TextBlock();
    Var_TextBox.Name = "TextBlock1";
    StackPanel_LayoutRoot.Children.Add(Var_TextBox);
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    object Var_TextBlock = FindName("TextBlock1");
}


Var_TextBlock=null

It returns a null value...؟
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jun-12 15:23pm    
Is it System.Windows.FrameworkTemplate.FindName, System.Windows.FrameworkElement.FindName or something else? you should provide sufficient detail when asking question, not making us to do guesswork.
--SA
Sergey Alexandrovich Kryukov 5-Jun-12 15:24pm    
What could be the sense of this horrific code? Why would you do that?
--SA
Sergey Alexandrovich Kryukov 5-Jun-12 15:28pm    
Are you sure that: you call the method with the UIElement containing this StackPanel_LayoutRoot? Are you sure first method is called first, before the second one?
The code sample is not complete...
--SA

Need to add a RegisterName call:

RegisterName("TextBlock1", Var_TextBox);


FindName operates within the current element's namescope. 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.
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.
 
Share this answer
 
As I say, this code sample is not complete to give a final answer. To get one idea on this matter, please see:
http://stackoverflow.com/questions/2285491/wpf-findname-returns-null-when-it-should-not[^].

—SA
 
Share this answer
 
Comments
Clifford Nelson 5-Jun-12 19:39pm    
The problem was that the name had never been registered, therefore could not find the name, see my solution.
I believe you are using the System.Windows.FrameworkElement.FindName() method. To do so, you should be calling it like so:

C#
StackPanel_LayoutRoot.FindName("TextBlock1");


Here is the MSDN link to how to use this method:

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx[^]
 
Share this answer
 
v2
Comments
Clifford Nelson 5-Jun-12 19:39pm    
The problem was that the name had never been registered, therefore could not find the name, see my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900