public Visual GetDescendantByType(Visual element, Type type)
{
if (element == null) return null;
if (element.GetType() == type) return element;
Visual foundElement = null;
if (element is FrameworkElement)
{
(element as FrameworkElement).ApplyTemplate();
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null)
break;
}
return foundElement;
}
private void listbox1_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
ScrollViewer ScrollViewer1 = GetDescendantByType(listbox1, typeof(ScrollViewer)) as ScrollViewer;
ScrollViewer ScrollViewer2 = GetDescendantByType(listbox2, typeof(ScrollViewer)) as ScrollViewer;
ScrollViewer2.ScrollToVerticalOffset(ScrollViewer1.VerticalOffset);
}