0

In my WPF project, I have a System.Windows.Controls.UserControl control. How to find a control inside that contol ?

Tony
  • 12,405
  • 36
  • 126
  • 226
  • Care to give a concrete piece of code to help with? There are many different ways of resolving children depending on whether you want to do it in XAML or code. – jjrdk Jul 01 '11 at 08:37
  • I want to do it on the server side code – Tony Jul 01 '11 at 08:41

2 Answers2

1

use VisualTree, if I understood your question correctly.

refer to msdn : http://msdn.microsoft.com/en-us/library/dd409789.aspx

Pradeep
  • 484
  • 5
  • 13
1

In that case you would probably want to walk the visual tree, like this extension method does:

internal static T FindVisualChild<T>(this DependencyObject parent) where T : DependencyObject
{
    if (parent == null)
    {
        return null;
    }

    DependencyObject parentObject = parent;
    int childCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childCount; i++)
    {
        DependencyObject childObject = VisualTreeHelper.GetChild(parentObject, i);
        if (childObject == null)
        {
            continue;
        }

        var child = childObject as T;
        return child ?? FindVisualChild<T>(childObject);
    }

    return null;
}

It requires that you know the type of the control you are looking for.

jjrdk
  • 1,882
  • 15
  • 18