Con el código siguiente se podrían hacer los cálculos necesarios al suscribirse al evento que salta al mover la barra de scroll. En definitiva se trata de añadir funcionalidad al propio listbox de Silverlight creando un nuevo control que acceda al ScrollBar que vienen implementado en el ListBox y del que no he conseguido acceder de cualquier otra manera.
using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; public class ccListBox: ListBox { public delegate void MyEventsEventHandler(object sender, ScrollBarChangedArgs e); public event MyEventsEventHandler ScrollBarChanged; private double _lastValue; public override void OnApplyTemplate() { base.OnApplyTemplate(); base.LayoutUpdated += new System.EventHandler(ccListBox_LayoutUpdated); } void ccListBox_LayoutUpdated(object sender, System.EventArgs e) { base.LayoutUpdated -= new System.EventHandler(ccListBox_LayoutUpdated); ScrollViewer viewer = GetTemplateChild("ScrollViewer") as ScrollViewer; if (viewer != null) { if (viewer.GetVisualChildren().Count() > 0) { FrameworkElement viewerRoot = (FrameworkElement)VisualTreeHelper.GetChild(viewer, 0); ScrollBar horizontal = ((ScrollBar)viewerRoot.FindName("HorizontalScrollBar")); if (horizontal != null) { horizontal.ValueChanged += ScrollBar_ValueChanged; } } } } void ScrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { ScrollBar bar = sender as ScrollBar; if (e.NewValue != _lastValue) { ScrollBarChangedArgs args = new ScrollBarChangedArgs(); args.Maximum = bar.Maximum; args.ViewPortSize = bar.ViewportSize; args.NewValue = e.NewValue; args.OldValue = e.OldValue; OnScrollBarChanged(this, args); } _lastValue = e.NewValue; } protected virtual void OnScrollBarChanged(object sender, ScrollBarChangedArgs e) { if (ScrollBarChanged != null) { ScrollBarChanged(sender, e); } } } public class ScrollBarChangedArgs : RoutedEventArgs { public double OldValue { get; set; } public double NewValue { get; set; } public double Maximum { get; set; } public double ViewPortSize { get; set; } }
Un saludo
Oscar
No hay comentarios:
Publicar un comentario