1

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 UserControls 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 ?

2 Answers2

1

Use method OfType to filter children by their type

        foreach (UserControl1 control in mystack.Children.OfType<UserControl1>())
        {

        }
Evgeny Gorbovoy
  • 765
  • 3
  • 20
  • but in my loop `foreach (UserControl1 control in mystack.Children)` doesn't it get the type automatically ? –  Mar 21 '18 at 19:19
  • Yes it does. OfType filter out all controls that are on of type UserControl1 – Evgeny Gorbovoy Mar 21 '18 at 19:21
  • @user9530640 Sorry, I somehow misunderstood your question. If you execute ```foreach (UserControl1 control in mystack.Children)``` and some of children are not of type UserControl1, then there will be some exception, probably cast exception or similar – Evgeny Gorbovoy Mar 08 '21 at 02:31
-2

What if do some reflection:

Type t = typeof(yourControl);
if (t == typeof(int)) // or whatever you need
    // Some code here

typeof takes a type name (which you specify at compile time). Or you can use GetType too:

if(yourcontrol.GetType() == typeof(yourControl))
//code here

GetType gets the runtime type of an instance.

rdi0r
  • 9
  • 4
  • before i try it , take a look at my post again , as per ur logic , should `foreach (UserControl1 control in mystack.Children)` do the same ? even this should get the `UserControl1`s only , right ? anyways, i'll give ur solution a try –  Mar 21 '18 at 19:13
  • wait....i don't see any loop in your answer ??? I need to loop through the controls , i mentioned that in the post clearly –  Mar 21 '18 at 19:13
  • typeof(instanceOfObjectHere) won't compile – Evgeny Gorbovoy Mar 21 '18 at 19:17
  • this is very wrong :( .... Please don't give misleading answers :) –  Mar 21 '18 at 19:19
  • I forgot to give a straitgh answer...i should use a foreach and put all your options there. I just said...use reflection same as Eugene did – rdi0r Mar 21 '18 at 19:41