I create a usercontrol and I add it to a grid.
BuildingBlock m_Bblock = new BuildingBlocks.BuildingBlock(2, 2);
m_grid.Children.Add(m_Bblock);
The constructor of my BuildingBlock has as input, the number of buttons In and Out, which both are usercontrols. Inside the constructor I have:
for (int kk = 0; kk < NumIn; ++kk)
{
InButton inbtn = new InButton();
inbtn.m_Button_Click += Button_In_Click;
InStackPanel.Children.Add(inbtn);
AllInBtn.Add(inbtn);
}
And similar code for OutButton.
I need to have information about the position of the first OutButton on the grid, so I used:
OutButton btn = m_Bblock.AllOutBtn[0];
Point pt = btn.TransformToAncestor(m_grid).Transform(new Point(0, 0));
This leads to an error ('The specified Visual is not a predecessor of this Visual'). I think this is due to the fact that OutButton is not yet "created" by the system, since:
Point pt = m_Bblock.TransformToAncestor(GridDropAccept).Transform(new Point(0, 0));
works and:
OutButton btn = m_Bblock.AllOutBtn[0];
Point pt = btn.TransformToAncestor(m_grid).Transform(new Point(0, 0));
works if I call this code later (for example by a button click, after the creation event).
Why this happens? How can I refresh the "creation" of my OutButton so that I can use it for my purposes?