Skip to content

Commit 24ae708

Browse files
Vijay-NirmalRosuavio
authored andcommitted
Fix issue when index is greater than Count and Big Fix for Sample Page
1 parent 54944a7 commit 24ae708

File tree

7 files changed

+84
-54
lines changed

7 files changed

+84
-54
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -439,7 +439,7 @@
439439
<Content Include="SamplePages\Menu\Menu.bind" />
440440
<Content Include="SamplePages\InAppNotification\InAppNotificationCode.bind" />
441441
<Content Include="SamplePages\InAppNotification\InAppNotificationXaml.bind" />
442-
<Content Include="SamplePages\ListViewExtensions\ListViewExtensionsCode.bind" />
442+
<Content Include="SamplePages\ListViewExtensions\ListViewExtensionsXaml.bind" />
443443
<Content Include="SamplePages\Implicit Animations\ImplicitAnimationsCode.bind" />
444444
<Content Include="SamplePages\DispatcherQueueHelper\DispatcherQueueHelperCode.bind" />
445445
<Content Include="SamplePages\TextToolbar\TextToolbar.bind" />
@@ -626,6 +626,7 @@
626626
<Content Include="SamplePages\Graph\LoginButtonXaml.bind" />
627627
<Content Include="SamplePages\Graph\PeoplePickerXaml.bind" />
628628
<Content Include="SamplePages\Graph\PersonViewXaml.bind" />
629+
<Content Include="SamplePages\ListViewExtensions\ListViewExtensionsCode.bind" />
629630
</ItemGroup>
630631
<ItemGroup>
631632
<Compile Include="App.xaml.cs">
Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,9 @@
1-
<Page
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages"
7-
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
8-
mc:Ignorable="d">
1+
<GridView x:Name="MyGridView"/>
92

10-
<Page.Resources>
11-
<local:SampleCommand x:Key="SampleCommand" />
3+
using Microsoft.Toolkit.Uwp.UI;
124

13-
<DataTemplate x:Name="NormalTemplate">
14-
<TextBlock Text="{Binding}" Foreground="Green" VerticalAlignment="Center" FontWeight="Bold"></TextBlock>
15-
</DataTemplate>
5+
// Scrolling with index
6+
await MyGridView.SmoothScrollIntoViewWithIndex(index: int, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int);
167

17-
<DataTemplate x:Name="AlternateTemplate">
18-
<TextBlock Text="{Binding}" Foreground="Red" VerticalAlignment="Center" FontWeight="Bold"></TextBlock>
19-
</DataTemplate>
20-
</Page.Resources>
21-
22-
<Grid>
23-
<TextBlock x:Name="IndexInput" Text="@[Index:Slider:100:0-500]" Visibility="Collapsed"></TextBlock>
24-
<TextBlock x:Name="ItemPlacementInput" Text="@[Item Placement:Enum:ItemPlacement.Bottom]" Visibility="Collapsed"></TextBlock>
25-
<TextBlock x:Name="DisableAnimationInput" Text="@[Disable Animation:Bool:False]" Visibility="Collapsed"></TextBlock>
26-
<TextBlock x:Name="ScrollIfVisibileInput" Text="@[Scroll If Visibile:Bool:False]" Visibility="Collapsed"></TextBlock>
27-
<TextBlock x:Name="AdditionalHorizontalOffsetInput" Text="@[Additional Horizontal Offset:Slider:0:-500-500]" Visibility="Collapsed"></TextBlock>
28-
<TextBlock x:Name="AdditionalVerticalOffsetInput" Text="@[Additional Vertical Offset:Slider:0:-500-500]" Visibility="Collapsed"></TextBlock>
29-
30-
<ListView
31-
x:Name="SampleListView"
32-
Margin="12"
33-
ItemTemplate="{StaticResource NormalTemplate}"
34-
IsItemClickEnabled="True"
35-
ui:ListViewExtensions.Command="{StaticResource SampleCommand}"
36-
ui:ListViewExtensions.AlternateColor="#33AAAAAA"
37-
ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}"
38-
ui:ListViewExtensions.ItemContainerStretchDirection="Both">
39-
</ListView>
40-
</Grid>
41-
</Page>
8+
// Scrolling with item
9+
await MyGridView.SmoothScrollIntoViewWithItem(item: object, itemPlacement: ItemPlacement, disableAnimation: bool, scrollIfVisibile: bool, additionalHorizontalOffset: int, additionalVerticalOffset: int);

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
</Page.Resources>
2121

2222
<Grid>
23-
<TextBlock x:Name="IndexInput" Text="[Index:String:100]" Visibility="Collapsed"></TextBlock>
2423
<ListView x:Name="SampleListView"
2524
Margin="12"
2625
ui:ListViewExtensions.AlternateColor="#33AAAAAA"

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListViewExtensions/ListViewExtensionsPage.xaml.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,42 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
1414
{
1515
public sealed partial class ListViewExtensionsPage : Page, IXamlRenderListener
1616
{
17+
private ListView sampleListView;
18+
private TextBlock indexInput;
19+
private TextBlock itemPlacementInput;
20+
private TextBlock disableAnimationInput;
21+
private TextBlock scrollIfVisibileInput;
22+
private TextBlock additionalHorizontalOffsetInput;
23+
private TextBlock additionalVerticalOffsetInput;
24+
1725
public ListViewExtensionsPage()
1826
{
1927
this.InitializeComponent();
28+
Load();
2029
}
2130

22-
public async void OnXamlRendered(FrameworkElement control)
31+
public void OnXamlRendered(FrameworkElement control)
2332
{
24-
var sampleListView = control.FindChild("SampleListView") as ListView;
25-
var indexInput = control.FindChild("IndexInput") as TextBlock;
26-
var itemPlacementInput = control.FindChild("ItemPlacementInput") as TextBlock;
27-
var disableAnimationInput = control.FindChild("DisableAnimationInput") as TextBlock;
28-
var scrollIfVisibileInput = control.FindChild("ScrollIfVisibileInput") as TextBlock;
29-
var additionalHorizontalOffsetInput = control.FindChild("AdditionalHorizontalOffsetInput") as TextBlock;
30-
var additionalVerticalOffsetInput = control.FindChild("AdditionalVerticalOffsetInput") as TextBlock;
33+
sampleListView = control.FindChild("SampleListView") as ListView;
34+
indexInput = control.FindChild("IndexInput") as TextBlock;
35+
itemPlacementInput = control.FindChild("ItemPlacementInput") as TextBlock;
36+
disableAnimationInput = control.FindChild("DisableAnimationInput") as TextBlock;
37+
scrollIfVisibileInput = control.FindChild("ScrollIfVisibileInput") as TextBlock;
38+
additionalHorizontalOffsetInput = control.FindChild("AdditionalHorizontalOffsetInput") as TextBlock;
39+
additionalVerticalOffsetInput = control.FindChild("AdditionalVerticalOffsetInput") as TextBlock;
40+
41+
if (sampleListView != null)
42+
{
43+
sampleListView.ItemsSource = GetOddEvenSource(201);
44+
}
45+
46+
// Transfer Data Context so we can access SampleCommand
47+
control.DataContext = this;
48+
}
3149

32-
SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (sender, args) =>
50+
private void Load()
51+
{
52+
SampleController.Current.RegisterNewCommand("Start Smooth Scroll", (_, __) =>
3353
{
3454
var index = int.Parse(indexInput.Text);
3555
var itemPlacement = (ItemPlacement)Enum.Parse(typeof(ItemPlacement), itemPlacementInput.Text);
@@ -50,7 +70,7 @@ public ObservableCollection<string> GetOddEvenSource(int count)
5070
{
5171
var oddEvenSource = new ObservableCollection<string>();
5272

53-
for (int number = 0; number <= count; number++)
73+
for (int number = 0; number < count; number++)
5474
{
5575
var item = (number % 2) == 0 ? $"{number} - Even" : $"{number} - Odd";
5676
oddEvenSource.Add(item);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Page
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages"
7+
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
8+
mc:Ignorable="d">
9+
10+
<Page.Resources>
11+
<local:SampleCommand x:Key="SampleCommand" />
12+
13+
<DataTemplate x:Name="NormalTemplate">
14+
<TextBlock Text="{Binding}" Foreground="Green" VerticalAlignment="Center" FontWeight="Bold"></TextBlock>
15+
</DataTemplate>
16+
17+
<DataTemplate x:Name="AlternateTemplate">
18+
<TextBlock Text="{Binding}" Foreground="Red" VerticalAlignment="Center" FontWeight="Bold"></TextBlock>
19+
</DataTemplate>
20+
</Page.Resources>
21+
22+
<Grid>
23+
<TextBlock x:Name="IndexInput" x:Load="False" Text="@[Index:Slider:100:0-200]" Visibility="Collapsed"></TextBlock>
24+
<TextBlock x:Name="ItemPlacementInput" x:Load="False" Text="@[Item Placement:Enum:ItemPlacement.Bottom]" Visibility="Collapsed"></TextBlock>
25+
<TextBlock x:Name="DisableAnimationInput" x:Load="False" Text="@[Disable Animation:Bool:False]" Visibility="Collapsed"></TextBlock>
26+
<TextBlock x:Name="ScrollIfVisibileInput" x:Load="False" Text="@[Scroll If Visibile:Bool:True]" Visibility="Collapsed"></TextBlock>
27+
<TextBlock x:Name="AdditionalHorizontalOffsetInput" x:Load="False" Text="@[Additional Horizontal Offset:Slider:0:-500-500]" Visibility="Collapsed"></TextBlock>
28+
<TextBlock x:Name="AdditionalVerticalOffsetInput" x:Load="False" Text="@[Additional Vertical Offset:Slider:0:-500-500]" Visibility="Collapsed"></TextBlock>
29+
30+
<ListView
31+
x:Name="SampleListView"
32+
Margin="12"
33+
ItemTemplate="{StaticResource NormalTemplate}"
34+
IsItemClickEnabled="True"
35+
ui:ListViewExtensions.Command="{StaticResource SampleCommand}"
36+
ui:ListViewExtensions.AlternateColor="#33AAAAAA"
37+
ui:ListViewExtensions.AlternateItemTemplate="{StaticResource AlternateTemplate}"
38+
ui:ListViewExtensions.ItemContainerStretchDirection="Both">
39+
</ListView>
40+
</Grid>
41+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,8 @@
11051105
"Type": "ListViewExtensionsPage",
11061106
"About": "Extensions for all controls that inherit from ListViewBase like ListView.",
11071107
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase",
1108-
"XamlCodeFile": "ListViewExtensionsCode.bind",
1108+
"CodeFile": "ListViewExtensionsCode.bind",
1109+
"XamlCodeFile": "ListViewExtensionsXaml.bind",
11091110
"Icon": "/Assets/Helpers.png",
11101111
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/ListViewExtensions.md"
11111112
},

Microsoft.Toolkit.Uwp.UI/Extensions/ListViewBase/SmoothScrollIntoView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static class SmoothScrollIntoView
2929
/// <returns>Note: Even though this return <see cref="Task"/>, it will not wait until the scrolling completes</returns>
3030
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)
3131
{
32-
if (index > listViewBase.Items.Count)
32+
if (index > (listViewBase.Items.Count - 1))
3333
{
34-
index = listViewBase.Items.Count;
34+
index = (listViewBase.Items.Count - 1);
3535
}
3636

3737
index = (index < 0) ? (index + listViewBase.Items.Count) : index;

0 commit comments

Comments
 (0)