Trong XAML bạn có thể truy cập vào ScrollViewer và thêm các sự kiện như thế này
<ListBox Name="listBox"
ScrollViewer.ScrollChanged="listBox_ScrollChanged"
Cập nhật
Đây là probablly những gì bạn cần trong Mã đằng sau
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);
}
}
Với một thực hiện 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);
}
}
}
Nguồn
2010-11-09 22:53:34
Câu trả lời rất hay. Tôi đã không có thời gian để thực hiện nó để xem nếu nó làm mọi thứ một cách chính xác, nhưng nó chắc chắn âm thanh đúng. Cảm ơn sự giúp đỡ của bạn. – riwalk