ComboBox 控件模板有一个 ScrollViewer。如何从 ComboBox 的实例中获取对它的引用?
我尝试将其命名为“ScrollViwer1”并使用它,但没有成功。
var scroll = FindVisualChildByName<ScrollViewer>(this.comboBox, "ScrollViewer1");
public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
{
return child as T;
}
else
{
T result = FindVisualChildByName<T>(child, name);
if (result != null)
return result;
}
}
return null;
}