Friday, September 21, 2012

How to find a Image using Image name(String) in Silverlight

How to find a Image using Image name(String) in Silverlight

I have created a page which contains several controls, in this i have to get a image which is in the page. I have the image name as string value. I have made a for loop to find the image and return, but it is tedious while looping all the controls in the page if it is more and it is getting much time too.

// Passing the string and find as image

Image imgBack = FindControl<Image>((UIElement)Layout, typeof(Image), strSelectedimg); 

// Function to find image

public T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement {         if (parent == null) return null;         if (parent.GetType() == targetType && ((T)parent).Name == ControlName)         {             return (T)parent;         }         T result = null;         int count = VisualTreeHelper.GetChildrenCount(parent);         for (int i = 0; i < count; i++)         {             UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);             if (FindControl<T>(child, targetType, ControlName) != null)             {                  result = FindControl<T>(child, targetType, ControlName);                  break;             }          }          return result;      }      

Is there any other easy way to find the image in the page using the string value.?

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog