WPF + Controls.UserControl. How to find a control inside?
In my WPF project, I have a System.Windows.Controls.UserControl control. How to find a 开发者_Go百科control inside that contol ?
use VisualTree, if I understood your question correctly.
refer to msdn : http://msdn.microsoft.com/en-us/library/dd409789.aspx
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.
精彩评论