Finding Dynamically generated Wcf Controls by FindName()

2k Views Asked by At

The FramworkElement.FindName() method of finding a control within a parent control seems like it should straight forward...

But I am upping the anty and it seems like the framework does not like what I'm trying to do.

First off, I do realize there are plenty of different ways of doing things and keep in mind this is the first form I am creating in WPF.

I am loading controls into a StackPanel based on the number of items in a collection. This method is a must as the collection is determined by the number of directories within a [user entered Url].

The list builds horizontal stackpanels into a vertical stackpanel that is placed on the form during design.

So dirStackPanel is on the form. I am inserting controlStackPanel into dirStackPanel n number of times and name each one with an identifying name: (string)("controlStackPanel" + n).

I am also filling the controlStackPanel with controls but that is a moot point considering that I can not retrieve the controlstackPanel from its parent (dirStackPanel) by name.

example:

var getPanel = (StackPanel) this.dirStackPanel.FindName((string)("controlStackPanel" + n))Returns Null

So to be clear, each control that I am inserting AT RUN TIME is being assigned a name and can easily be retrieved using a loop. But the FindName method will not work on the first child control of the parent "StackPanel". It keeps returning a null and not the object.

Any Ideas?

1

There are 1 best solutions below

0
On

If you are creating the controls dynamically, you have to call RegisterName first to be able to find them.

For example:

dirStackPanel.RegisterName(controlStackPanel.Name, controlStackPanel);