in XAML è possibile accedere al ScrollViewer e aggiungere eventi come questo
<ListBox Name="listBox"
ScrollViewer.ScrollChanged="listBox_ScrollChanged"
Aggiornamento
Questo è probablly quello che ti serve in codice dietro
List<ScrollBar> scrollBarList = GetVisualChildCollection<ScrollBar>(listBox);
foreach (ScrollBar scrollBar in scrollBarList)
{
if (scrollBar.Orientation == Orientation.Horizontal)
{
scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(listBox_HorizontalScrollBar_ValueChanged);
}
else
{
scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(listBox_VerticalScrollBar_ValueChanged);
}
}
Con un'implementazione di GetVisualChildCollection
public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual
{
List<T> visualCollection = new List<T>();
GetVisualChildCollection(parent as DependencyObject, visualCollection);
return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
{
visualCollection.Add(child as T);
}
else if (child != null)
{
GetVisualChildCollection(child, visualCollection);
}
}
}
fonte
2010-11-09 22:53:34
Risposta molto bella. Non ho avuto il tempo di implementarlo per vedere se fa tutto correttamente, ma sicuramente suona bene. Grazie per l'aiuto. – riwalk