asusralis
Topic Author
Posts: 142
Joined: 30 Jul 2018, 05:03

Cannot find child name in Grid.

02 Dec 2018, 02:02

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
Last edited by asusralis on 02 Dec 2018, 02:03, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Cannot find child name in Grid.

02 Dec 2018, 16:14

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");
 
asusralis
Topic Author
Posts: 142
Joined: 30 Jul 2018, 05:03

Re: Cannot find child name in Grid.

02 Dec 2018, 23:13

Awesome, thanks a lot.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Cannot find child name in Grid.

03 Dec 2018, 14:33

You're welcome.

Who is online

Users browsing this forum: Google [Bot] and 70 guests