@@ -20,20 +20,25 @@ public static class SmoothScrollIntoView
20
20
/// Smooth scrolling the list to bring the specified index into view
21
21
/// </summary>
22
22
/// <param name="listViewBase">List to scroll</param>
23
- /// <param name="index">The intex to bring into view</param>
23
+ /// <param name="index">The index to bring into view. Index can be negative. </param>
24
24
/// <param name="itemPlacement">Set the item placement after scrolling</param>
25
25
/// <param name="disableAnimation">Set true to disable animation</param>
26
- /// <param name="scrollIfVisibile ">Set true to disable scrolling when the corresponding item is in view</param>
26
+ /// <param name="scrollIfVisible ">Set false to disable scrolling when the corresponding item is in view</param>
27
27
/// <param name="additionalHorizontalOffset">Adds additional horizontal offset</param>
28
28
/// <param name="additionalVerticalOffset">Adds additional vertical offset</param>
29
29
/// <returns>Note: Even though this return <see cref="Task"/>, it will not wait until the scrolling completes</returns>
30
- public static async Task SmoothScrollIntoViewWithIndex ( this ListViewBase listViewBase , int index , ItemPlacement itemPlacement = ItemPlacement . Default , bool disableAnimation = false , bool scrollIfVisibile = true , int additionalHorizontalOffset = 0 , int additionalVerticalOffset = 0 )
30
+ public static async Task SmoothScrollIntoViewWithIndex ( this ListViewBase listViewBase , int index , ItemPlacement itemPlacement = ItemPlacement . Default , bool disableAnimation = false , bool scrollIfVisible = true , int additionalHorizontalOffset = 0 , int additionalVerticalOffset = 0 )
31
31
{
32
32
if ( index > ( listViewBase . Items . Count - 1 ) )
33
33
{
34
34
index = listViewBase . Items . Count - 1 ;
35
35
}
36
36
37
+ if ( index < - listViewBase . Items . Count )
38
+ {
39
+ index = - listViewBase . Items . Count ;
40
+ }
41
+
37
42
index = ( index < 0 ) ? ( index + listViewBase . Items . Count ) : index ;
38
43
39
44
bool isVirtualizing = default ;
@@ -42,6 +47,8 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
42
47
var scrollViewer = listViewBase . FindDescendant < ScrollViewer > ( ) ;
43
48
var selectorItem = listViewBase . ContainerFromIndex ( index ) as SelectorItem ;
44
49
50
+ // If selectorItem is null then the panel is virtualized.
51
+ // So in order to get the container of the item we need to scroll to that item first and then use ContainerFromIndex
45
52
if ( selectorItem == null )
46
53
{
47
54
isVirtualizing = true ;
@@ -70,6 +77,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
70
77
var transform = selectorItem . TransformToVisual ( ( UIElement ) scrollViewer . Content ) ;
71
78
var position = transform . TransformPoint ( new Point ( 0 , 0 ) ) ;
72
79
80
+ // Scrolling back to previous position
73
81
if ( isVirtualizing )
74
82
{
75
83
var tcs = new TaskCompletionSource < object > ( ) ;
@@ -104,7 +112,8 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
104
112
105
113
double finalXPosition , finalYPosition ;
106
114
107
- if ( ! scrollIfVisibile && ( previousXOffset <= maxXPosition && previousXOffset >= minXPosition ) && ( previousYOffset <= maxYPosition && previousYOffset >= minYPosition ) )
115
+ // If the Item is in view and scrollIfVisible is false then we don't need to scroll
116
+ if ( ! scrollIfVisible && ( previousXOffset <= maxXPosition && previousXOffset >= minXPosition ) && ( previousYOffset <= maxYPosition && previousYOffset >= minYPosition ) )
108
117
{
109
118
finalXPosition = previousXOffset ;
110
119
finalYPosition = previousYOffset ;
@@ -152,7 +161,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie
152
161
finalYPosition = maxYPosition + additionalVerticalOffset ;
153
162
break ;
154
163
155
- case ItemPlacement . Centre :
164
+ case ItemPlacement . Center :
156
165
var centreX = ( listViewBaseWidth - selectorItemWidth ) / 2.0 ;
157
166
var centreY = ( listViewBaseHeight - selectorItemHeight ) / 2.0 ;
158
167
finalXPosition = maxXPosition - centreX + additionalHorizontalOffset ;
0 commit comments