I have two UserControl
with just one/two textblocks.On my window, i add the first UserControl
couple o' times in a for each loop and the 2nd user control,i just add it in the end,which mean i have only 1 UserControl2
.
public class test
{
UserControl1 btn = new UserControl1;
private void thread1()
{
foreach (var item in mycollection)///i am not including the actual iteration target because it is a class and then the post might be too huge
{
mystack.Children.Add(btn);
}
mystack.Children.Add(new UserControl2);
}
Note that i am adding only the UserControl1
in the foreach
loop but i am adding UserControl2
outside the loop,which means i am adding it for once.
Anyway,i may iterate through all the controls i added to mystack
in a foreach
loop like :
foreach (var control in mystack.Children)
{
////My codes here
}
As i mentioned earlier,there are 2 types of UserControl
s added to the StackPanel
.How do i only iterate through one type of UserControl
only ? I mean what if i want to iterate through only the UserControl1
s from the Stackpanel
(mystack) ?
I tried something like :
private void thread2()
{
foreach (UserControl1 control in mystack.Children)
{
}
//////Or
for (i = o; i <= mystack.children - 1; i++)
{
btn.height = 10 /// my other codes here :)
}}
But both of them throw Unable to cast object of type UserControl2 to UserControl1
exception which means it is iterating through all the controls(both UserControl1
and UserControl2
) :(
See the first code black in the post?Some might suggest doing whatever i wanna do in the first foreach
loop but i cant,this has to be done in different threads(first loop in thread1
and the rest in thread2
...So,how should i achieve this ?