tags:

views:

101

answers:

3

How to determine the element type in WPF programatically?

For example my xaml is consisting of textbox, radio buttons, combo's, list boxes etc.

In the load event, say I want to do something related to the controls.(Say for all textbox,

the foreground color will be red, for all labels the background color will be green)..

something of that sort.

So I have to loop through the entire list of controls present in the Xaml and then have to

write the control specific logic.

Is it by using Framework element?

Please give the code in c#. For example please take 3/4 controls of your choice.

I am also searching in google!

Thanks in advance

A: 

GetType() should work if you have a reference to the control/type.

Gishu
can you please give a full code say with 3/4 controls and how to obtain the same from a collection. Means my xaml is consisting of textbox, radio buttons, conbo's, list boxes etc. In the load event , say I want to do something related to the controls. So I have to loop through the entire list of controls and then have to write the control specific logic
priyanka.sarkar
+2  A: 

if you only have a finite number of types to check against try casting using the "as" operator and then checking for null.

Button button = control as Button;
if (button != null)
{
  // this is a button)
}
...

The as operator will not throw an exception if the cast cannot be done.


EDIT: IF you are only trying to achieve styling of the controls you should look at the <Style/> tag.

See here for a good example

benPearce
can you please give a full code say with 3/4 controls and how to obtain the same from a collection. Means my xaml is consisting of textbox, radio buttons, conbo's, list boxes etc. In the load event , say I want to do something related to the controls. So I have to loop through the entire list of controls and then have to write the control specific logic
priyanka.sarkar
A: 

You can use:

if (element is Grid)
{
}
else if (element is Label)
...
Konstantin
can you please give a full code say with 3/4 controls and how to obtain the same from a collection.Means my xaml is consisting of textbox, radio buttons, conbo's, list boxes etc.In the load event , say I want to do something related to the controls.So I have to loop through the entire list of controls and then have to write the control specific logic.
priyanka.sarkar