I add an element at runtime programmaticaly:
private void AddModule(string modulename, string ip, string description)
{
ModuleInfobox infobox = new ModuleInfobox();
infobox.Modulename = modulename;
infobox.IpAddress = ip;
infobox.Description = description;
infobox.Modulenamecolor = "Lime";
infobox.Name = modulename;
ModulePanel.RegisterName(infobox.Name, infobox);
ModulePanel.Children.Add(infobox);
}
ModuleInfoBox is my UserControl
. Now I need a reference to this object. I try this:
private FindLogicalNodeDelegate delFindLogicalNode;
private FindNameDelegate delFindName;
delegate object FindLogicalNodeDelegate(DependencyObject wantedObject, string name);
delegate object FindNameDelegate(string name);
delFindLogicalNode = new FindLogicalNodeDelegate(LogicalTreeHelper.FindLogicalNode);
delFindName = new FindNameDelegate(ModulePanel.FindName);
In a thread:
object wantedObject = ModulePanel.Dispatcher.BeginInvoke(delFindLogicalNode, DispatcherPriority.Normal, ModulePanel, "modulename").Result;
object wantedObject2 = this.Dispatcher.BeginInvoke(delFindName, DispatcherPriority.Normal, "moduleName").Result;
When I use the debugger, FindLogicalNode found sometimes a reference but usually not. I read these topics: WPF - FindName Returns null when it should not and "FindName" doesn't work if an element added in code but still there must be an error in my code