ComboBox控件模板有一个ScrollViewer。我如何从一个组合框的实例中获得对它的引用?
我试着将它命名为"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;
}
你可以使用FrameworkTemplate。FindName方法。
ScrollViewer sv = comboBox.Template.FindName("DropDownScrollViewer", comboBox) as ScrollViewer;
if (sv != null)
{
// do something...
}