How to identify the particular type of children on a container control
This article helps to identify the particular type of children on a container control using GetType method and how to change controls properties using sender of the method and How to get and set Row number of children on Grid Container control in WPF.
Identifying particular type of children on container control
Many times we require to identify the particular types children on a container control in our c# Coding. Here i am using for loop to loop through all the controls in container conrols Grid(WPF). Any container controls has property children use that method to catch particular children and use GetType() method to identify the type of the children.
Compare the GetType method result with our required children type. Here in this example i am searching for image controls and i want to decrement the row number of the image control on WPF Grid Container Control.
Here after getting the image control type children i am checking grid.Row number and based on condition if it is less than my condition i am setting Grid.Row value.
In Wpf to get the Chidren row number on a Grid Container control use Grid.GetRow(Children) Property.
To set the Children row number on a Grid Container Control use
Grid.SetRow(children,rownumber);
for (int i = 0; i < Grid.Children.Count; i++)
{
if (Grid.Children[i].GetType().ToString() == "System.Windows.Controls.Image")
{
Image img = ((Image)Grid.Children[i]);//converting children to image control type(instance creation of image type)
if (Grid.GetRow(img) >= currrownumber)//Getting children grid row number
{
Grid.SetRow(img, Grid.GetRow(img) - 1);//setting Children grid row number
}
}
}
I hope it will be helpful for you.
Thanks.