Sometimes you need to add some elements to the children collection of some container, for example, panel.
So you just write down my_panel.Add( child );
Later you find out that you need to find this element using it's name. You have specified it even before adding element to the container, so you expect it to be found be calling FindName method of the container.
But when you call FindName and provide it the name of the added in runtime element, it returns just null. Note: in case with elements, added throung the XAML this method works fine.
So what is the difference? The name has been set and the element is in logical children collection - it just must work.
After a short investigation it turned out that FindName method does not take elements children collection content into account - it get a INameScope and uses it to resolve the element by it's name. So after you add element in runtime, you should do the following:
System.Windows.NameScope.GetNameScope( elements parent ).RegisterName(name, object to link name with );
Hope it will help someone :)
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5