如何访问 WPF 中 ComboBox 内的 ScrollViewer? [英] How can I get access to the ScrollViewer inside a ComboBox in WPF?
本文介绍了如何访问 WPF 中 ComboBox 内的 ScrollViewer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ComboBox 控件模板有一个 ScrollViewer.如何从 ComboBox 的实例中获取对它的引用?
The ComboBox Control Template has a ScrollViewer. How can I get a reference to it from an instance of a ComboBox?
我尝试将其命名为ScrollViwer1"并使用它,但没有成功.
I tried naming it "ScrollViwer1" and using this, but I had no success.
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...
}
这篇关于如何访问 WPF 中 ComboBox 内的 ScrollViewer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文