问题
I am attempting to get notified when an element is scrolled into view ,
As i understand it , ScrollViewer calls BringIntoView on it's child elements and then the child elements raise a RequestBringIntoView event which the ScrollViewer later catches and handles.
This is done by Registering a Class handler for this Event and catching it as it bubbles up
EventManager.RegisterClassHandler(typeof(ScrollViewer), RequestBringIntoViewEvent, new RequestBringIntoViewEventHandler(OnRequestBringIntoView));
ScrollViewer Source Code
What i need is to get notified each time this process takes place , my attempt is to derive from ScrollViewer and Create my own event of type and approach it in one of 2 ways :
1) Add a class handler for that same event with a true at the end for handling Handled events .
2) Creating my own event which handles events of type RequestBringIntoViewEvent
public class NotifyOnBringIntoViewScrollViewer : ScrollViewer
{
static NotifyOnBringIntoViewScrollViewer()
{
// for secnario (2) replace RequestBringIntoViewEvent with Noti
EventManager.RegisterClassHandler(typeof(NotifyOnBringIntoViewScrollViewer),RequestBringIntoViewEvent , new RequestBringIntoViewEventHandler(OnRequestBringIntoView), true);
}
public static readonly RoutedEvent NotifyBringIntoViewEvent = EventManager.RegisterRoutedEvent(
"NotifyBringIntoView", RoutingStrategy.Direct, typeof(RequestBringIntoViewEventHandler), typeof(NotifyOnBringIntoViewScrollViewer));
private static void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
// notify ..
}
}
The handler is never reached , for one or tow reasons ,
1) I miss understood the way routed events works in regard to registering handlers . 2) My child elements never raise RequestBringIntoView , my attempt for this is as follows :
cs :
public List<int> Items
{
get
{
return new List<int>
{
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25
};
}
}
xaml :
<local:NotifyOnBringIntoViewScrollViewer>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</local:NotifyOnBringIntoViewScrollViewer>
FYI : VirtualizingStackPanel , i put this because i wasn't sure that this is the default panel and if with out Virtualizing BringIntoView is ever called , since they are already in view.
来源:https://stackoverflow.com/questions/19475908/scrollviewer-notify-on-requestbringintoview