tags:

views:

158

answers:

1

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

A: 

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 lst = new List();

        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);
            }

        }
    }
jolly