方法:
///relate 表示组合控件的父控件,即最外层
///type 要查找的控件类型
///resElement 找到的控件
private void FindChildByType(DependencyObject relate, Type type, ref FrameworkElement resElement)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(relate); i++)
{
var el = VisualTreeHelper.GetChild(relate, i) as FrameworkElement;
if (el.GetType() == type)
{
resElement = el;
return;
}
else
{
FindChildByType(el, type, ref resElement);
}
}
}
调用:
FrameworkElement efind = default(FrameworkElement);
FindChildByType(lbItem, typeof(TextBlock), ref efind);