diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index a281bd38fb8..783cf333e27 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -15,8 +15,8 @@ public sealed partial class ListViewExtensionsPage : Page, IXamlRenderListener private ListView sampleListView; private TextBlock indexInput; private TextBlock itemPlacementInput; - private TextBlock disableAnimationInput; - private TextBlock scrollIfVisibileInput; + private CheckBox disableAnimationInput; + private CheckBox scrollIfVisibileInput; private TextBlock additionalHorizontalOffsetInput; private TextBlock additionalVerticalOffsetInput; @@ -31,8 +31,8 @@ public void OnXamlRendered(FrameworkElement control) sampleListView = control.FindChildByName("SampleListView") as ListView; indexInput = control.FindChildByName("IndexInput") as TextBlock; itemPlacementInput = control.FindChildByName("ItemPlacementInput") as TextBlock; - disableAnimationInput = control.FindChildByName("DisableAnimationInput") as TextBlock; - scrollIfVisibileInput = control.FindChildByName("ScrollIfVisibileInput") as TextBlock; + disableAnimationInput = control.FindChildByName("DisableAnimationInput") as CheckBox; + scrollIfVisibileInput = control.FindChildByName("ScrollIfVisibileInput") as CheckBox; additionalHorizontalOffsetInput = control.FindChildByName("AdditionalHorizontalOffsetInput") as TextBlock; additionalVerticalOffsetInput = control.FindChildByName("AdditionalVerticalOffsetInput") as TextBlock; @@ -46,12 +46,12 @@ private void Load() { SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (sender, args) => { - var index = int.Parse(indexInput.Text); - var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput.Text); - var disableAnimation = bool.Parse(disableAnimationInput.Text); - var scrollIfVisibile = bool.Parse(scrollIfVisibileInput.Text); - var additionalHorizontalOffset = int.Parse(additionalHorizontalOffsetInput.Text); - var additionalVerticalOffset = int.Parse(additionalVerticalOffsetInput.Text); + var index = int.Parse(indexInput?.Text); + var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput?.Text); + var disableAnimation = disableAnimationInput?.IsChecked ?? false; + var scrollIfVisibile = scrollIfVisibileInput?.IsChecked ?? true; + var additionalHorizontalOffset = int.Parse(additionalHorizontalOffsetInput?.Text); + var additionalVerticalOffset = int.Parse(additionalVerticalOffsetInput?.Text); sampleListView.SmoothScrollIntoViewWithIndex(index, itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); }); } diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind index 3c51b383832..c5701d5a021 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind @@ -19,8 +19,8 @@ - - + + diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs index d0efc73cc0f..d8d749c6946 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs @@ -25,9 +25,9 @@ public enum ItemPlacement Top, /// - /// Aligned centre + /// Aligned center /// - Centre, + Center, /// /// Aligned right diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs index 7dfcbd45f01..ef5d9a9ed63 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs @@ -20,20 +20,25 @@ public static class SmoothScrollIntoView /// Smooth scrolling the list to bring the specified index into view /// /// List to scroll - /// The intex to bring into view + /// The index to bring into view. Index can be negative. /// Set the item placement after scrolling /// Set true to disable animation - /// Set true to disable scrolling when the corresponding item is in view + /// Set false to disable scrolling when the corresponding item is in view /// Adds additional horizontal offset /// Adds additional vertical offset /// Note: Even though this return , it will not wait until the scrolling completes - 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) + 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) { if (index > (listViewBase.Items.Count - 1)) { index = listViewBase.Items.Count - 1; } + if (index < -listViewBase.Items.Count) + { + index = -listViewBase.Items.Count; + } + index = (index < 0) ? (index + listViewBase.Items.Count) : index; bool isVirtualizing = default; @@ -42,6 +47,8 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie var scrollViewer = listViewBase.FindDescendant(); var selectorItem = listViewBase.ContainerFromIndex(index) as SelectorItem; + // If selectorItem is null then the panel is virtualized. + // So in order to get the container of the item we need to scroll to that item first and then use ContainerFromIndex if (selectorItem == null) { isVirtualizing = true; @@ -70,6 +77,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie var transform = selectorItem.TransformToVisual((UIElement)scrollViewer.Content); var position = transform.TransformPoint(new Point(0, 0)); + // Scrolling back to previous position if (isVirtualizing) { var tcs = new TaskCompletionSource(); @@ -104,7 +112,8 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie double finalXPosition, finalYPosition; - if (!scrollIfVisibile && (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) && (previousYOffset <= maxYPosition && previousYOffset >= minYPosition)) + // If the Item is in view and scrollIfVisible is false then we don't need to scroll + if (!scrollIfVisible && (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) && (previousYOffset <= maxYPosition && previousYOffset >= minYPosition)) { finalXPosition = previousXOffset; finalYPosition = previousYOffset; @@ -152,7 +161,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie finalYPosition = maxYPosition + additionalVerticalOffset; break; - case ItemPlacement.Centre: + case ItemPlacement.Center: var centreX = (listViewBaseWidth - selectorItemWidth) / 2.0; var centreY = (listViewBaseHeight - selectorItemHeight) / 2.0; finalXPosition = maxXPosition - centreX + additionalHorizontalOffset;