From 5aa3a84ca59e778de41fd33e23419a60db669b7b Mon Sep 17 00:00:00 2001 From: Vijay Nirmal Date: Sat, 22 Feb 2020 01:59:19 +0530 Subject: [PATCH 01/13] Added SmoothScrollIntoView ListViewBase Extension Method --- .../Models/Sample.cs | 4 +- .../ListViewExtensionsCode.bind | 10 +- .../ListViewExtensionsPage.xaml | 10 +- .../ListViewExtensionsPage.xaml.cs | 39 +++- .../Extensions/ListViewBase/ItemPlacement.cs | 42 ++++ .../ListViewBase/SmoothScrollIntoView.cs | 198 ++++++++++++++++++ 6 files changed, 290 insertions(+), 13 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs b/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs index f57a430d307..43ce1887d0d 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs @@ -23,6 +23,7 @@ using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.Input.GazeInteraction; using Microsoft.Toolkit.Uwp.SampleApp.Models; +using Microsoft.Toolkit.Uwp.UI; using Microsoft.Toolkit.Uwp.UI.Animations; using Microsoft.Toolkit.Uwp.UI.Controls; using Microsoft.Toolkit.Uwp.UI.Media; @@ -674,8 +675,9 @@ private static Type LookForTypeByName(string typeName) // TODO Reintroduce graph controls // typeof(UserToPersonConverter)) // Search in Microsoft.Toolkit.Graph.Controls + ItemPlacement.Default.GetType(), // Search in Microsoft.Toolkit.Uwp.UI EasingType.Default.GetType(), // Microsoft.Toolkit.Uwp.UI.Animations - ImageBlendMode.Multiply.GetType(), // Search in Microsoft.Toolkit.Uwp.UI + ImageBlendMode.Multiply.GetType(), // Search in Microsoft.Toolkit.Uwp.UI.Media Interaction.Enabled.GetType(), // Microsoft.Toolkit.Uwp.Input.GazeInteraction DataGridGridLinesVisibility.None.GetType(), // Microsoft.Toolkit.Uwp.UI.Controls.DataGrid GridSplitter.GridResizeDirection.Auto.GetType(), // Microsoft.Toolkit.Uwp.UI.Controls.Layout diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind index 8d475644188..40d42164cf2 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind @@ -8,15 +8,21 @@ - + - + + + + + + + - + - + - + new DelegateCommand(OnExecuteSampleCommand); + public ICommand SampleCommand => new DelegateCommand(OnExecuteSampleCommand); public async void OnXamlRendered(FrameworkElement control) { var sampleListView = control.FindChild("SampleListView") as ListView; + var indexInput = control.FindChild("IndexInput") as TextBlock; + var itemPlacementInput = control.FindChild("ItemPlacementInput") as TextBlock; + var disableAnimationInput = control.FindChild("DisableAnimationInput") as TextBlock; + var scrollIfVisibileInput = control.FindChild("ScrollIfVisibileInput") as TextBlock; + var additionalHorizontalOffsetInput = control.FindChild("AdditionalHorizontalOffsetInput") as TextBlock; + var additionalVerticalOffsetInput = control.FindChild("AdditionalVerticalOffsetInput") as TextBlock; + + 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); + sampleListView.SmoothScrollIntoViewWithIndex(index, itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); + }); if (sampleListView != null) { - sampleListView.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync(); + sampleListView.ItemsSource = GetOddEvenSource(500); } // Transfer Data Context so we can access SampleCommand control.DataContext = this; } - private async void OnExecuteSampleCommand(PhotoDataItem item) + private async void OnExecuteSampleCommand(string item) + { + await new MessageDialog($"You clicked {item} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync(); + } + + public ObservableCollection GetOddEvenSource(int count) { - await new MessageDialog($"You clicked {item.Title} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync(); + var oddEvenSource = new ObservableCollection(); + + for (int number = 0; number <= count; number++) + { + var item = (number % 2) == 0 ? $"{number} - Even" : $"{number} - Odd"; + oddEvenSource.Add(item); + } + + return oddEvenSource; } } } \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs new file mode 100644 index 00000000000..e22b490f9bb --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.Toolkit.Uwp.UI +{ + /// + /// Item Position + /// + public enum ItemPlacement + { + /// + /// If visible then it will not scroll, if not then item will be aligned to the nearest edge + /// + Default, + + /// + /// Aligned left + /// + Left, + + /// + /// Aligned top + /// + Top, + + /// + /// Aligned centre + /// + Centre, + + /// + /// Aligned right + /// + Right, + + /// + /// Aligned bottom + /// + Bottom + } +} diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs new file mode 100644 index 00000000000..b06b53ad8ed --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs @@ -0,0 +1,198 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading.Tasks; +using Windows.Foundation; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; + +namespace Microsoft.Toolkit.Uwp.UI +{ + /// + /// Smooth scroll the list to bring specified item into view + /// + public static class SmoothScrollIntoView + { + /// + /// Smooth scrolling the list to bring the specified index into view + /// + /// List to scroll + /// The intex to bring into view + /// Set the item placement after scrolling + /// Set true to disable animation + /// Set true 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) + { + if (index > listViewBase.Items.Count) + { + index = listViewBase.Items.Count; + } + + index = (index < 0) ? (index + listViewBase.Items.Count) : index; + + bool isVirtualizing = default; + double previousXOffset = default, previousYOffset = default; + + var scrollViewer = listViewBase.FindDescendant(); + var selectorItem = listViewBase.ContainerFromIndex(index) as SelectorItem; + + if (selectorItem == null) + { + isVirtualizing = true; + + previousXOffset = scrollViewer.HorizontalOffset; + previousYOffset = scrollViewer.VerticalOffset; + + var tcs = new TaskCompletionSource(); + + void viewChanged(object _, ScrollViewerViewChangedEventArgs __) => tcs.TrySetResult(result: null); + + try + { + scrollViewer.ViewChanged += viewChanged; + listViewBase.ScrollIntoView(listViewBase.Items[index], ScrollIntoViewAlignment.Leading); + await tcs.Task; + } + finally + { + scrollViewer.ViewChanged -= viewChanged; + } + + selectorItem = (SelectorItem)listViewBase.ContainerFromIndex(index); + } + + var transform = selectorItem.TransformToVisual((UIElement)scrollViewer.Content); + var position = transform.TransformPoint(new Point(0, 0)); + + if (isVirtualizing) + { + var tcs = new TaskCompletionSource(); + + void viewChanged(object _, ScrollViewerViewChangedEventArgs __) => tcs.TrySetResult(result: null); + + try + { + scrollViewer.ViewChanged += viewChanged; + scrollViewer.ChangeView(previousXOffset, previousYOffset, zoomFactor: null, disableAnimation: true); + await tcs.Task; + } + finally + { + scrollViewer.ViewChanged -= viewChanged; + } + } + + var listViewBaseWidth = listViewBase.ActualWidth; + var selectorItemWidth = selectorItem.ActualWidth; + var listViewBaseHeight = listViewBase.ActualHeight; + var selectorItemHeight = selectorItem.ActualHeight; + + previousXOffset = scrollViewer.HorizontalOffset; + previousYOffset = scrollViewer.VerticalOffset; + + var minXPosition = position.X - listViewBaseWidth + selectorItemWidth; + var minYPosition = position.Y - listViewBaseHeight + selectorItemHeight; + + var maxXPosition = position.X; + var maxYPosition = position.Y; + + double finalXPosition, finalYPosition; + + if (!scrollIfVisibile && (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) && (previousYOffset <= maxYPosition && previousYOffset >= minYPosition)) + { + finalXPosition = previousXOffset; + finalYPosition = previousYOffset; + } + else + { + switch (itemPlacement) + { + case ItemPlacement.Default: + if (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) + { + finalXPosition = previousXOffset + additionalHorizontalOffset; + } + else if (Math.Abs(previousXOffset - minXPosition) < Math.Abs(previousXOffset - maxXPosition)) + { + finalXPosition = minXPosition + additionalHorizontalOffset; + } + else + { + finalXPosition = maxXPosition + additionalHorizontalOffset; + } + + if (previousYOffset <= maxYPosition && previousYOffset >= minYPosition) + { + finalYPosition = previousYOffset + additionalVerticalOffset; + } + else if (Math.Abs(previousYOffset - minYPosition) < Math.Abs(previousYOffset - maxYPosition)) + { + finalYPosition = minYPosition + additionalVerticalOffset; + } + else + { + finalYPosition = maxYPosition + additionalVerticalOffset; + } + + break; + + case ItemPlacement.Left: + finalXPosition = maxXPosition + additionalHorizontalOffset; + finalYPosition = previousYOffset + additionalVerticalOffset; + break; + + case ItemPlacement.Top: + finalXPosition = previousXOffset + additionalHorizontalOffset; + finalYPosition = maxYPosition + additionalVerticalOffset; + break; + + case ItemPlacement.Centre: + var centreX = (listViewBaseWidth - selectorItemWidth) / 2.0; + var centreY = (listViewBaseHeight - selectorItemHeight) / 2.0; + finalXPosition = maxXPosition - centreX + additionalHorizontalOffset; + finalYPosition = maxYPosition - centreY + additionalVerticalOffset; + break; + + case ItemPlacement.Right: + finalXPosition = minXPosition + additionalHorizontalOffset; + finalYPosition = previousYOffset + additionalVerticalOffset; + break; + + case ItemPlacement.Bottom: + finalXPosition = previousXOffset + additionalHorizontalOffset; + finalYPosition = minYPosition + additionalVerticalOffset; + break; + + default: + finalXPosition = previousXOffset + additionalHorizontalOffset; + finalYPosition = previousYOffset + additionalVerticalOffset; + break; + } + } + + scrollViewer.ChangeView(finalXPosition, finalYPosition, zoomFactor: null, disableAnimation); + } + + /// + /// Smooth scrolling the list to bring the specified data item into view + /// + /// List to scroll + /// The data item to bring into view + /// Set the item placement after scrolling + /// Set true to disable animation + /// Set true 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 SmoothScrollIntoViewWithItem(this ListViewBase listViewBase, object item, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisibile = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) + { + await SmoothScrollIntoViewWithIndex(listViewBase, listViewBase.Items.IndexOf(item), itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); + } + } +} From 54944a7405de01a3096d0f4a4987aa4e00a4d218 Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Mon, 24 May 2021 17:25:54 -0400 Subject: [PATCH 02/13] Use SampleCommand class dont change DataContext --- .../ListViewExtensionsCode.bind | 5 ++- .../ListViewExtensionsPage.xaml | 5 ++- .../ListViewExtensionsPage.xaml.cs | 38 +++++++++++++------ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind index 40d42164cf2..20ca981d158 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind @@ -3,10 +3,13 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages" xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" mc:Ignorable="d"> + + @@ -29,7 +32,7 @@ Margin="12" ItemTemplate="{StaticResource NormalTemplate}" IsItemClickEnabled="True" - ui:ListViewExtensions.Command="{Binding SampleCommand}" + ui:ListViewExtensions.Command="{StaticResource SampleCommand}" ui:ListViewExtensions.AlternateColor="#33AAAAAA" ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}" ui:ListViewExtensions.ItemContainerStretchDirection="Both"> diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index 681f952e3d6..273cc364eb3 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -2,11 +2,14 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages" xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> + + @@ -22,7 +25,7 @@ Margin="12" ui:ListViewExtensions.AlternateColor="#33AAAAAA" ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}" - ui:ListViewExtensions.Command="{Binding SampleCommand}" + ui:ListViewExtensions.Command="{StaticResource SampleCommand}" ui:ListViewExtensions.ItemContainerStretchDirection="Both" IsItemClickEnabled="True" ItemTemplate="{StaticResource NormalTemplate}" /> diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index e9c76813c1d..23942085f5e 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -5,8 +5,6 @@ using System; using System.Collections.ObjectModel; using System.Windows.Input; -using Microsoft.Toolkit.Uwp.SampleApp.Common; -using Microsoft.Toolkit.Uwp.SampleApp.Data; using Microsoft.Toolkit.Uwp.UI; using Windows.UI.Popups; using Windows.UI.Xaml; @@ -21,8 +19,6 @@ public ListViewExtensionsPage() this.InitializeComponent(); } - public ICommand SampleCommand => new DelegateCommand(OnExecuteSampleCommand); - public async void OnXamlRendered(FrameworkElement control) { var sampleListView = control.FindChild("SampleListView") as ListView; @@ -48,14 +44,6 @@ public async void OnXamlRendered(FrameworkElement control) { sampleListView.ItemsSource = GetOddEvenSource(500); } - - // Transfer Data Context so we can access SampleCommand - control.DataContext = this; - } - - private async void OnExecuteSampleCommand(string item) - { - await new MessageDialog($"You clicked {item} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync(); } public ObservableCollection GetOddEvenSource(int count) @@ -71,4 +59,30 @@ public ObservableCollection GetOddEvenSource(int count) return oddEvenSource; } } + +#pragma warning disable SA1402 // File may only contain a single class + internal class SampleCommand : ICommand +#pragma warning restore SA1402 // File may only contain a single class + { + event EventHandler ICommand.CanExecuteChanged + { + add { } + remove { } + } + + public bool CanExecute(object parameter) => true; + + public void Execute(object parameter) + { + if (parameter is string s) + { + OnExecuteSampleCommand(s); + } + } + + private static async void OnExecuteSampleCommand(string item) + { + await new MessageDialog($"You clicked {item} via the 'ListViewExtensions.Command' binding", "Item Clicked").ShowAsync(); + } + } } \ No newline at end of file From 24ae708c9406078fad9210a8f9110fea4def1c02 Mon Sep 17 00:00:00 2001 From: Vijay Nirmal Date: Sat, 22 Feb 2020 19:05:53 +0530 Subject: [PATCH 03/13] Fix issue when index is greater than Count and Big Fix for Sample Page --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 5 ++- .../ListViewExtensionsCode.bind | 44 +++---------------- .../ListViewExtensionsPage.xaml | 1 - .../ListViewExtensionsPage.xaml.cs | 40 ++++++++++++----- .../ListViewExtensionsXaml.bind | 41 +++++++++++++++++ .../SamplePages/samples.json | 3 +- .../ListViewBase/SmoothScrollIntoView.cs | 4 +- 7 files changed, 84 insertions(+), 54 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index c5287b6b82c..696298e3c8f 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -1,4 +1,4 @@ - + Debug @@ -439,7 +439,7 @@ - + @@ -626,6 +626,7 @@ + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind index 20ca981d158..370629b0742 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind @@ -1,41 +1,9 @@ - + - - +using Microsoft.Toolkit.Uwp.UI; - - - +// Scrolling with index +await MyGridView.SmoothScrollIntoViewWithIndex(index: int, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); - - - - - - - - - - - - - - - - - \ No newline at end of file +// Scrolling with item +await MyGridView.SmoothScrollIntoViewWithItem(item: object, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index 273cc364eb3..7107a732929 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -20,7 +20,6 @@ - + private void Load() + { + SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (_, __) => { var index = int.Parse(indexInput.Text); var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput.Text); @@ -50,7 +70,7 @@ public ObservableCollection GetOddEvenSource(int count) { var oddEvenSource = new ObservableCollection(); - for (int number = 0; number <= count; number++) + for (int number = 0; number < count; number++) { var item = (number % 2) == 0 ? $"{number} - Even" : $"{number} - Odd"; oddEvenSource.Add(item); diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind new file mode 100644 index 00000000000..12d8f5a4ea1 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index 437e8a44333..89734b82abe 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -1105,7 +1105,8 @@ "Type": "ListViewExtensionsPage", "About": "Extensions for all controls that inherit from ListViewBase like ListView.", "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase", - "XamlCodeFile": "ListViewExtensionsCode.bind", + "CodeFile": "ListViewExtensionsCode.bind", + "XamlCodeFile": "ListViewExtensionsXaml.bind", "Icon": "/Assets/Helpers.png", "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/ListViewExtensions.md" }, diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs index b06b53ad8ed..01a5ca989b9 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs @@ -29,9 +29,9 @@ public static class SmoothScrollIntoView /// 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) { - if (index > listViewBase.Items.Count) + if (index > (listViewBase.Items.Count - 1)) { - index = listViewBase.Items.Count; + index = (listViewBase.Items.Count - 1); } index = (index < 0) ? (index + listViewBase.Items.Count) : index; From af4d2027fbb3502d8989a5cb453d3df0836e0af0 Mon Sep 17 00:00:00 2001 From: Vijay Nirmal Date: Tue, 25 Feb 2020 00:55:42 +0530 Subject: [PATCH 04/13] Updated Default ItemPlacement value --- .../SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind index 12d8f5a4ea1..9499d984a5c 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind @@ -21,7 +21,7 @@ - + From 60fc7c5cb5af91a6c4589d8bac6b627b51aeb3a4 Mon Sep 17 00:00:00 2001 From: Vijay Nirmal Date: Sun, 5 Apr 2020 13:04:58 +0530 Subject: [PATCH 05/13] Fixes for Pipeline build failed --- .../ListViewExtensionsPage.xaml.cs | 6 +++--- .../ListViewBase/SmoothScrollIntoView.cs | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 5f4b61b6766..81ddfc6e428 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -49,7 +49,7 @@ public void OnXamlRendered(FrameworkElement control) private void Load() { - SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (_, __) => + SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (sender, args) => { var index = int.Parse(indexInput.Text); var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput.Text); @@ -66,7 +66,7 @@ private void Load() } } - public ObservableCollection GetOddEvenSource(int count) + private ObservableCollection GetOddEvenSource(int count) { var oddEvenSource = new ObservableCollection(); diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs index 01a5ca989b9..050c46887f5 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs @@ -17,7 +17,7 @@ namespace Microsoft.Toolkit.Uwp.UI public static class SmoothScrollIntoView { /// - /// Smooth scrolling the list to bring the specified index into view + /// Smooth scrolling the list to bring the specified index into view /// /// List to scroll /// The intex to bring into view @@ -31,7 +31,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie { if (index > (listViewBase.Items.Count - 1)) { - index = (listViewBase.Items.Count - 1); + index = listViewBase.Items.Count - 1; } index = (index < 0) ? (index + listViewBase.Items.Count) : index; @@ -51,17 +51,17 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie var tcs = new TaskCompletionSource(); - void viewChanged(object _, ScrollViewerViewChangedEventArgs __) => tcs.TrySetResult(result: null); + void ViewChanged(object obj, ScrollViewerViewChangedEventArgs args) => tcs.TrySetResult(result: null); try { - scrollViewer.ViewChanged += viewChanged; + scrollViewer.ViewChanged += ViewChanged; listViewBase.ScrollIntoView(listViewBase.Items[index], ScrollIntoViewAlignment.Leading); await tcs.Task; } finally { - scrollViewer.ViewChanged -= viewChanged; + scrollViewer.ViewChanged -= ViewChanged; } selectorItem = (SelectorItem)listViewBase.ContainerFromIndex(index); @@ -74,17 +74,17 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie { var tcs = new TaskCompletionSource(); - void viewChanged(object _, ScrollViewerViewChangedEventArgs __) => tcs.TrySetResult(result: null); + void ViewChanged(object obj, ScrollViewerViewChangedEventArgs args) => tcs.TrySetResult(result: null); try { - scrollViewer.ViewChanged += viewChanged; + scrollViewer.ViewChanged += ViewChanged; scrollViewer.ChangeView(previousXOffset, previousYOffset, zoomFactor: null, disableAnimation: true); await tcs.Task; } finally { - scrollViewer.ViewChanged -= viewChanged; + scrollViewer.ViewChanged -= ViewChanged; } } @@ -180,7 +180,7 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie } /// - /// Smooth scrolling the list to bring the specified data item into view + /// Smooth scrolling the list to bring the specified data item into view /// /// List to scroll /// The data item to bring into view From 2531a00537f58e66a26108753f3abc16e89d7d15 Mon Sep 17 00:00:00 2001 From: Vijay Nirmal Date: Mon, 13 Apr 2020 01:18:24 +0530 Subject: [PATCH 06/13] Review changes --- .../ListViewExtensionsPage.xaml.cs | 20 +++++++++---------- .../ListViewExtensionsXaml.bind | 4 ++-- .../Extensions/ListViewBase/ItemPlacement.cs | 4 ++-- .../ListViewBase/SmoothScrollIntoView.cs | 19 +++++++++++++----- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 81ddfc6e428..1d8e4d5a388 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -17,8 +17,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; @@ -33,8 +33,8 @@ public void OnXamlRendered(FrameworkElement control) sampleListView = control.FindChild("SampleListView") as ListView; indexInput = control.FindChild("IndexInput") as TextBlock; itemPlacementInput = control.FindChild("ItemPlacementInput") as TextBlock; - disableAnimationInput = control.FindChild("DisableAnimationInput") as TextBlock; - scrollIfVisibileInput = control.FindChild("ScrollIfVisibileInput") as TextBlock; + disableAnimationInput = control.FindChild("DisableAnimationInput") as CheckBox; + scrollIfVisibileInput = control.FindChild("ScrollIfVisibileInput") as CheckBox; additionalHorizontalOffsetInput = control.FindChild("AdditionalHorizontalOffsetInput") as TextBlock; additionalVerticalOffsetInput = control.FindChild("AdditionalVerticalOffsetInput") as TextBlock; @@ -51,12 +51,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 9499d984a5c..a305b62926b 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind @@ -22,8 +22,8 @@ - - + + diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs index e22b490f9bb..0a35a311bdb 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 050c46887f5..777835ce229 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; From 8e6ef0af180df21554b36ba0f93eebd4d6730cc7 Mon Sep 17 00:00:00 2001 From: Vijay Nirmal Date: Sun, 12 Jul 2020 00:42:37 +0530 Subject: [PATCH 07/13] Review Changes - Files Reorg --- ...cs => ListViewExtensions.AlternateRows.cs} | 25 ----------- ...istViewExtensions.SmoothScrollIntoView.cs} | 2 +- ...ListViewExtensions.StretchItemContainer.cs | 44 +++++++++++++++++++ 3 files changed, 45 insertions(+), 26 deletions(-) rename Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/{ListViewExtensions.cs => ListViewExtensions.AlternateRows.cs} (84%) rename Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/{SmoothScrollIntoView.cs => ListViewExtensions.SmoothScrollIntoView.cs} (99%) create mode 100644 Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.StretchItemContainer.cs diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.AlternateRows.cs similarity index 84% rename from Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs rename to Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.AlternateRows.cs index c9ad2e4cbf9..53d7355371f 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.AlternateRows.cs @@ -28,11 +28,6 @@ public static partial class ListViewExtensions /// public static readonly DependencyProperty AlternateItemTemplateProperty = DependencyProperty.RegisterAttached("AlternateItemTemplate", typeof(DataTemplate), typeof(ListViewExtensions), new PropertyMetadata(null, OnAlternateItemTemplatePropertyChanged)); - /// - /// Attached for setting the container content stretch direction on the - /// - public static readonly DependencyProperty ItemContainerStretchDirectionProperty = DependencyProperty.RegisterAttached("ItemContainerStretchDirection", typeof(ItemContainerStretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnItemContainerStretchDirectionPropertyChanged)); - /// /// Gets the alternate associated with the specified /// @@ -73,26 +68,6 @@ public static void SetAlternateItemTemplate(Windows.UI.Xaml.Controls.ListViewBas obj.SetValue(AlternateItemTemplateProperty, value); } - /// - /// Gets the stretch associated with the specified - /// - /// The to get the associated from - /// The associated with the - public static ItemContainerStretchDirection GetItemContainerStretchDirection(Windows.UI.Xaml.Controls.ListViewBase obj) - { - return (ItemContainerStretchDirection)obj.GetValue(ItemContainerStretchDirectionProperty); - } - - /// - /// Sets the stretch associated with the specified - /// - /// The to associate the with - /// The for binding to the - public static void SetItemContainerStretchDirection(Windows.UI.Xaml.Controls.ListViewBase obj, ItemContainerStretchDirection value) - { - obj.SetValue(ItemContainerStretchDirectionProperty, value); - } - private static void OnAlternateColorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { Windows.UI.Xaml.Controls.ListViewBase listViewBase = sender as Windows.UI.Xaml.Controls.ListViewBase; diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs similarity index 99% rename from Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs rename to Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs index 777835ce229..75628d1c995 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs @@ -14,7 +14,7 @@ namespace Microsoft.Toolkit.Uwp.UI /// /// Smooth scroll the list to bring specified item into view /// - public static class SmoothScrollIntoView + public static partial class ListViewExtensions { /// /// Smooth scrolling the list to bring the specified index into view diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.StretchItemContainer.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.StretchItemContainer.cs new file mode 100644 index 00000000000..3097c02fbb9 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.StretchItemContainer.cs @@ -0,0 +1,44 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Media; + +namespace Microsoft.Toolkit.Uwp.UI +{ + /// + /// Provides attached dependency properties for the + /// + public static partial class ListViewExtensions + { + /// + /// Attached for setting the container content stretch direction on the + /// + public static readonly DependencyProperty ItemContainerStretchDirectionProperty = DependencyProperty.RegisterAttached("ItemContainerStretchDirection", typeof(ItemContainerStretchDirection), typeof(ListViewExtensions), new PropertyMetadata(null, OnItemContainerStretchDirectionPropertyChanged)); + + /// + /// Gets the stretch associated with the specified + /// + /// The to get the associated from + /// The associated with the + public static ItemContainerStretchDirection GetItemContainerStretchDirection(Windows.UI.Xaml.Controls.ListViewBase obj) + { + return (ItemContainerStretchDirection)obj.GetValue(ItemContainerStretchDirectionProperty); + } + + /// + /// Sets the stretch associated with the specified + /// + /// The to associate the with + /// The for binding to the + public static void SetItemContainerStretchDirection(Windows.UI.Xaml.Controls.ListViewBase obj, ItemContainerStretchDirection value) + { + obj.SetValue(ItemContainerStretchDirectionProperty, value); + } + } +} From 2ebe4b2fec30feb858bed044af1ceb2cdc20b794 Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Mon, 28 Jun 2021 12:20:05 -0400 Subject: [PATCH 08/13] Make scrool controls part of the sample --- .../ListViewExtensionsPage.xaml | 36 ++++++++++++++++ .../ListViewExtensionsPage.xaml.cs | 40 +++++++++++------- .../ListViewExtensionsXaml.bind | 42 ++++++++++++++++--- 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index 7107a732929..4e81fb93152 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -20,7 +20,12 @@ + + + + + + + + + Default + Left + Top + Center + Right + Bottom + + + + + + \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 1d8e4d5a388..4eef27f345c 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -15,12 +15,12 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages public sealed partial class ListViewExtensionsPage : Page, IXamlRenderListener { private ListView sampleListView; - private TextBlock indexInput; - private TextBlock itemPlacementInput; + private TextBox indexInput; + private ComboBox itemPlacementInput; private CheckBox disableAnimationInput; private CheckBox scrollIfVisibileInput; - private TextBlock additionalHorizontalOffsetInput; - private TextBlock additionalVerticalOffsetInput; + private TextBox additionalHorizontalOffsetInput; + private TextBox additionalVerticalOffsetInput; public ListViewExtensionsPage() { @@ -31,12 +31,12 @@ public ListViewExtensionsPage() public void OnXamlRendered(FrameworkElement control) { sampleListView = control.FindChild("SampleListView") as ListView; - indexInput = control.FindChild("IndexInput") as TextBlock; - itemPlacementInput = control.FindChild("ItemPlacementInput") as TextBlock; + indexInput = control.FindChild("IndexInput") as TextBox; + itemPlacementInput = control.FindChild("ItemPlacementInput") as ComboBox; disableAnimationInput = control.FindChild("DisableAnimationInput") as CheckBox; scrollIfVisibileInput = control.FindChild("ScrollIfVisibileInput") as CheckBox; - additionalHorizontalOffsetInput = control.FindChild("AdditionalHorizontalOffsetInput") as TextBlock; - additionalVerticalOffsetInput = control.FindChild("AdditionalVerticalOffsetInput") as TextBlock; + additionalHorizontalOffsetInput = control.FindChild("AdditionalHorizontalOffsetInput") as TextBox; + additionalVerticalOffsetInput = control.FindChild("AdditionalVerticalOffsetInput") as TextBox; if (sampleListView != null) { @@ -51,12 +51,22 @@ 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 = disableAnimationInput?.IsChecked ?? false; - var scrollIfVisibile = scrollIfVisibileInput?.IsChecked ?? true; - var additionalHorizontalOffset = int.Parse(additionalHorizontalOffsetInput?.Text); - var additionalVerticalOffset = int.Parse(additionalVerticalOffsetInput?.Text); + var index = int.TryParse(indexInput.Text, out var i) ? i : 0; + var itemPlacement = itemPlacementInput.SelectedItem switch + { + "Default" => UI.ItemPlacement.Default, + "Left" => UI.ItemPlacement.Left, + "Top" => UI.ItemPlacement.Top, + "Center" => UI.ItemPlacement.Center, + "Right" => UI.ItemPlacement.Right, + "Bottom" => UI.ItemPlacement.Bottom, + _ => UI.ItemPlacement.Default + }; + + var disableAnimation = disableAnimationInput.IsChecked ?? false; + var scrollIfVisibile = scrollIfVisibileInput.IsChecked ?? true; + var additionalHorizontalOffset = int.TryParse(additionalHorizontalOffsetInput.Text, out var ho) ? ho : 0; + var additionalVerticalOffset = int.TryParse(additionalVerticalOffsetInput.Text, out var vo) ? vo : 0; 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 a305b62926b..b323642055a 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind @@ -20,15 +20,14 @@ - - - - - - + + + + + + + + + Default + Left + Top + Center + Right + Bottom + + + + + + \ No newline at end of file From 91367b8b85dc2335d16240c7c64717baebc406c5 Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Mon, 28 Jun 2021 12:25:31 -0400 Subject: [PATCH 09/13] Name SmoothScroll methods with Async --- .../ListViewExtensions/ListViewExtensionsCode.bind | 4 ++-- .../ListViewExtensions/ListViewExtensionsPage.xaml.cs | 2 +- .../ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind index 370629b0742..884ba387db2 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind @@ -3,7 +3,7 @@ using Microsoft.Toolkit.Uwp.UI; // Scrolling with index -await MyGridView.SmoothScrollIntoViewWithIndex(index: int, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); +await MyGridView.SmoothScrollIntoViewWithIndexAsync(index: int, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); // Scrolling with item -await MyGridView.SmoothScrollIntoViewWithItem(item: object, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); +await MyGridView.SmoothScrollIntoViewWithItemAsync(item: object, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 4eef27f345c..03b8b97d85e 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -67,7 +67,7 @@ private void Load() var scrollIfVisibile = scrollIfVisibileInput.IsChecked ?? true; var additionalHorizontalOffset = int.TryParse(additionalHorizontalOffsetInput.Text, out var ho) ? ho : 0; var additionalVerticalOffset = int.TryParse(additionalVerticalOffsetInput.Text, out var vo) ? vo : 0; - sampleListView.SmoothScrollIntoViewWithIndex(index, itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); + sampleListView.SmoothScrollIntoViewWithIndexAsync(index, itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); }); if (sampleListView != null) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs index 75628d1c995..da5b7346c28 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs @@ -27,7 +27,7 @@ public static partial class ListViewExtensions /// 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 scrollIfVisible = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) + public static async Task SmoothScrollIntoViewWithIndexAsync(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)) { @@ -199,9 +199,9 @@ public static async Task SmoothScrollIntoViewWithIndex(this ListViewBase listVie /// 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 SmoothScrollIntoViewWithItem(this ListViewBase listViewBase, object item, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisibile = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) + public static async Task SmoothScrollIntoViewWithItemAsync(this ListViewBase listViewBase, object item, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisibile = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) { - await SmoothScrollIntoViewWithIndex(listViewBase, listViewBase.Items.IndexOf(item), itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); + await SmoothScrollIntoViewWithIndexAsync(listViewBase, listViewBase.Items.IndexOf(item), itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); } } } From 65af1d2d68e030adc2c1384b202f9567c5499250 Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Mon, 28 Jun 2021 12:50:52 -0400 Subject: [PATCH 10/13] Remove over sepcified naming --- .../ListViewExtensionsPage.xaml.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 03b8b97d85e..c7c3ad9b7e1 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -54,13 +54,13 @@ private void Load() var index = int.TryParse(indexInput.Text, out var i) ? i : 0; var itemPlacement = itemPlacementInput.SelectedItem switch { - "Default" => UI.ItemPlacement.Default, - "Left" => UI.ItemPlacement.Left, - "Top" => UI.ItemPlacement.Top, - "Center" => UI.ItemPlacement.Center, - "Right" => UI.ItemPlacement.Right, - "Bottom" => UI.ItemPlacement.Bottom, - _ => UI.ItemPlacement.Default + "Default" => ItemPlacement.Default, + "Left" => ItemPlacement.Left, + "Top" => ItemPlacement.Top, + "Center" => ItemPlacement.Center, + "Right" => ItemPlacement.Right, + "Bottom" => ItemPlacement.Bottom, + _ => ItemPlacement.Default }; var disableAnimation = disableAnimationInput.IsChecked ?? false; From 05e250c3953d52a66730063e234cbb778f443216 Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Tue, 29 Jun 2021 11:06:16 -0400 Subject: [PATCH 11/13] Use smooth scrool controls in static xaml file --- .../ListViewExtensionsPage.xaml | 18 +++--- .../ListViewExtensionsPage.xaml.cs | 24 ++------ .../ListViewExtensionsXaml.bind | 59 +++---------------- 3 files changed, 24 insertions(+), 77 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index 4e81fb93152..3b61d30401c 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -24,15 +24,15 @@ - + + + { - var index = int.TryParse(indexInput.Text, out var i) ? i : 0; - var itemPlacement = itemPlacementInput.SelectedItem switch + var index = int.TryParse(IndexInput.Text, out var i) ? i : 0; + var itemPlacement = ItemPlacementInput.SelectedItem switch { "Default" => ItemPlacement.Default, "Left" => ItemPlacement.Left, @@ -63,10 +51,10 @@ private void Load() _ => ItemPlacement.Default }; - var disableAnimation = disableAnimationInput.IsChecked ?? false; - var scrollIfVisibile = scrollIfVisibileInput.IsChecked ?? true; - var additionalHorizontalOffset = int.TryParse(additionalHorizontalOffsetInput.Text, out var ho) ? ho : 0; - var additionalVerticalOffset = int.TryParse(additionalVerticalOffsetInput.Text, out var vo) ? vo : 0; + var disableAnimation = DisableAnimationInput.IsChecked ?? false; + var scrollIfVisibile = ScrollIfVisibileInput.IsChecked ?? true; + var additionalHorizontalOffset = int.TryParse(AdditionalHorizontalOffsetInput.Text, out var ho) ? ho : 0; + var additionalVerticalOffset = int.TryParse(AdditionalVerticalOffsetInput.Text, out var vo) ? vo : 0; sampleListView.SmoothScrollIntoViewWithIndexAsync(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 b323642055a..0c3c8ea384b 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsXaml.bind @@ -18,54 +18,13 @@ - - - - - - - - - - - - - - Default - Left - Top - Center - Right - Bottom - - - - - - - + + \ No newline at end of file From e46bdde0af7d35caead321ff352b3ce29a886030 Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Tue, 29 Jun 2021 11:37:58 -0400 Subject: [PATCH 12/13] Raname ItemPlacement to ScrollItemPlacement --- Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs | 2 +- .../ListViewExtensionsCode.bind | 4 ++-- .../ListViewExtensionsPage.xaml.cs | 14 +++++++------- .../ListViewExtensions.SmoothScrollIntoView.cs | 16 ++++++++-------- .../{ItemPlacement.cs => ScrollItemPlacement.cs} | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) rename Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/{ItemPlacement.cs => ScrollItemPlacement.cs} (96%) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs b/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs index 43ce1887d0d..9be588da6be 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs @@ -675,7 +675,7 @@ private static Type LookForTypeByName(string typeName) // TODO Reintroduce graph controls // typeof(UserToPersonConverter)) // Search in Microsoft.Toolkit.Graph.Controls - ItemPlacement.Default.GetType(), // Search in Microsoft.Toolkit.Uwp.UI + ScrollItemPlacement.Default.GetType(), // Search in Microsoft.Toolkit.Uwp.UI EasingType.Default.GetType(), // Microsoft.Toolkit.Uwp.UI.Animations ImageBlendMode.Multiply.GetType(), // Search in Microsoft.Toolkit.Uwp.UI.Media Interaction.Enabled.GetType(), // Microsoft.Toolkit.Uwp.Input.GazeInteraction diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind index 884ba387db2..22f495a7fad 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsCode.bind @@ -3,7 +3,7 @@ using Microsoft.Toolkit.Uwp.UI; // Scrolling with index -await MyGridView.SmoothScrollIntoViewWithIndexAsync(index: int, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); +await MyGridView.SmoothScrollIntoViewWithIndexAsync(index: int, itemPlacement: ScrollItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); // Scrolling with item -await MyGridView.SmoothScrollIntoViewWithItemAsync(item: object, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); +await MyGridView.SmoothScrollIntoViewWithItemAsync(item: object, itemPlacement: ScrollItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int); diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs index 8a0a3a021f6..b79849ef196 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs @@ -42,13 +42,13 @@ private void Load() var index = int.TryParse(IndexInput.Text, out var i) ? i : 0; var itemPlacement = ItemPlacementInput.SelectedItem switch { - "Default" => ItemPlacement.Default, - "Left" => ItemPlacement.Left, - "Top" => ItemPlacement.Top, - "Center" => ItemPlacement.Center, - "Right" => ItemPlacement.Right, - "Bottom" => ItemPlacement.Bottom, - _ => ItemPlacement.Default + "Default" => ScrollItemPlacement.Default, + "Left" => ScrollItemPlacement.Left, + "Top" => ScrollItemPlacement.Top, + "Center" => ScrollItemPlacement.Center, + "Right" => ScrollItemPlacement.Right, + "Bottom" => ScrollItemPlacement.Bottom, + _ => ScrollItemPlacement.Default }; var disableAnimation = DisableAnimationInput.IsChecked ?? false; diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs index da5b7346c28..419471d959b 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ListViewExtensions.SmoothScrollIntoView.cs @@ -27,7 +27,7 @@ public static partial class ListViewExtensions /// 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 SmoothScrollIntoViewWithIndexAsync(this ListViewBase listViewBase, int index, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisible = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) + public static async Task SmoothScrollIntoViewWithIndexAsync(this ListViewBase listViewBase, int index, ScrollItemPlacement itemPlacement = ScrollItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisible = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) { if (index > (listViewBase.Items.Count - 1)) { @@ -122,7 +122,7 @@ public static async Task SmoothScrollIntoViewWithIndexAsync(this ListViewBase li { switch (itemPlacement) { - case ItemPlacement.Default: + case ScrollItemPlacement.Default: if (previousXOffset <= maxXPosition && previousXOffset >= minXPosition) { finalXPosition = previousXOffset + additionalHorizontalOffset; @@ -151,29 +151,29 @@ public static async Task SmoothScrollIntoViewWithIndexAsync(this ListViewBase li break; - case ItemPlacement.Left: + case ScrollItemPlacement.Left: finalXPosition = maxXPosition + additionalHorizontalOffset; finalYPosition = previousYOffset + additionalVerticalOffset; break; - case ItemPlacement.Top: + case ScrollItemPlacement.Top: finalXPosition = previousXOffset + additionalHorizontalOffset; finalYPosition = maxYPosition + additionalVerticalOffset; break; - case ItemPlacement.Center: + case ScrollItemPlacement.Center: var centreX = (listViewBaseWidth - selectorItemWidth) / 2.0; var centreY = (listViewBaseHeight - selectorItemHeight) / 2.0; finalXPosition = maxXPosition - centreX + additionalHorizontalOffset; finalYPosition = maxYPosition - centreY + additionalVerticalOffset; break; - case ItemPlacement.Right: + case ScrollItemPlacement.Right: finalXPosition = minXPosition + additionalHorizontalOffset; finalYPosition = previousYOffset + additionalVerticalOffset; break; - case ItemPlacement.Bottom: + case ScrollItemPlacement.Bottom: finalXPosition = previousXOffset + additionalHorizontalOffset; finalYPosition = minYPosition + additionalVerticalOffset; break; @@ -199,7 +199,7 @@ public static async Task SmoothScrollIntoViewWithIndexAsync(this ListViewBase li /// 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 SmoothScrollIntoViewWithItemAsync(this ListViewBase listViewBase, object item, ItemPlacement itemPlacement = ItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisibile = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) + public static async Task SmoothScrollIntoViewWithItemAsync(this ListViewBase listViewBase, object item, ScrollItemPlacement itemPlacement = ScrollItemPlacement.Default, bool disableAnimation = false, bool scrollIfVisibile = true, int additionalHorizontalOffset = 0, int additionalVerticalOffset = 0) { await SmoothScrollIntoViewWithIndexAsync(listViewBase, listViewBase.Items.IndexOf(item), itemPlacement, disableAnimation, scrollIfVisibile, additionalHorizontalOffset, additionalVerticalOffset); } diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ScrollItemPlacement.cs similarity index 96% rename from Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs rename to Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ScrollItemPlacement.cs index 0a35a311bdb..4b5e2a583fd 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ItemPlacement.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/ScrollItemPlacement.cs @@ -7,7 +7,7 @@ namespace Microsoft.Toolkit.Uwp.UI /// /// Item Position /// - public enum ItemPlacement + public enum ScrollItemPlacement { /// /// If visible then it will not scroll, if not then item will be aligned to the nearest edge From c6212fc4fad1a9e20d00d5bb53166f6bce22aace Mon Sep 17 00:00:00 2001 From: Rosario Pulella Date: Fri, 2 Jul 2021 11:35:15 -0400 Subject: [PATCH 13/13] Sample fixes As per https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3222#issuecomment-872534456 --- .../ListViewExtensions/ListViewExtensionsPage.xaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml index 3b61d30401c..ae16844700f 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml @@ -34,11 +34,12 @@ ItemTemplate="{StaticResource NormalTemplate}" /> - + + + Text="100" />