Page 1 of 1

Cannot find child name in Grid.

Posted: 02 Dec 2018, 02:02
by asusralis
I'm currently working on a navigation system. When a view is told be be shown, the navigation first looks if a control of that name already exists. If not, it creates the element, gives it a name, and adds it to the children of the root. If it does exist, it will simply enable and show that instead of creating a new one.

The problem is that I cannot find any children I've added.
control.Name = "Name";

Root.Children.Add(control);

Root.FindName("Name");
I cannot seem to find an object of that name. If I create the view in XAML and give it an x:Name attribute, FindName does work. Furthermore, when I look up all children, it says the source is null.
IEnumerable<object> c = Root.Children.OfType<object>();
https://i.imgur.com/K7zTRjp.png

Re: Cannot find child name in Grid.

Posted: 02 Dec 2018, 16:14
by sfernandez
To programatically register objects by name in the UI tree you have to call RegisterName().
That is what XAML parser does when it finds the x:Name attribute in a node.
Root.RegisterName("Name", control);
Root.Children.Add(control);
Control control = (Control)Root.FindName("Name");

Re: Cannot find child name in Grid.

Posted: 02 Dec 2018, 23:13
by asusralis
Awesome, thanks a lot.

Re: Cannot find child name in Grid.

Posted: 03 Dec 2018, 14:33
by sfernandez
You're welcome.