jolly
jolly

Reputation: 137

How to find child control added in a style in silverlight 4?

Hi I have ContentControl and I am applying style in which I have ListBox. I want to find ListBoxItem in Xaml.cs.

Upvotes: 0

Views: 1956

Answers (1)

jolly
jolly

Reputation: 137

I have found out a solution. I have Implemented a method to find the visual element which takes two parameters, parent element and type of a control need to be find. Before calling this method I have used ApplyTemplate method

public static FrameworkElement[] FindDownInTree(FrameworkElement parent, Type controlType)
{
    List<FrameworkElement> lst = new List<FrameworkElement>();

    FindDownInTree(lst, parent, controlType);

    if (lst.Count > 0)
        return lst.ToArray();

    return null;
}



private static void FindDownInTree(List<FrameworkElement> lstElem, DependencyObject parent, Type controlType)
{

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        DependencyObject visual = VisualTreeHelper.GetChild(parent, i);

        if (controlType.IsInstanceOfType(visual))
        {
            lstElem.Add(visual as FrameworkElement);
        }

        if (visual != null)
        {
            FindDownInTree(lstElem, visual, controlType);
        }

    }
}

Upvotes: 1

Related Questions