Skip to content

Commit b31c819

Browse files
Merge pull request #3213 from deanchalk/AdaptiveGridImprovements
Improvements to AdaptiveGrid Sample page
2 parents 1ee6d34 + 66443b5 commit b31c819

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/AdaptiveGridView/AdaptiveGridViewCode.bind

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<Grid>
2525
<controls:AdaptiveGridView Name="AdaptiveGridViewControl"
26+
StretchContentForSingleRow="@[StretchContentForSingleRow:Bool:false]"
2627
OneRowModeEnabled="@[OneRowModeEnabled:Bool:false]"
2728
ItemHeight="@[ItemHeight:Slider:200:50-500]"
2829
DesiredWidth="@[DesiredWidth:Slider:300:50-500]"

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/AdaptiveGridView/AdaptiveGridViewPage.xaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,36 @@
2626
<Grid.RowDefinitions>
2727
<RowDefinition Height="Auto" />
2828
<RowDefinition Height="*" />
29+
<RowDefinition Height="Auto" />
2930
</Grid.RowDefinitions>
31+
<Grid.ColumnDefinitions>
32+
<ColumnDefinition Width="Auto" />
33+
<ColumnDefinition Width="*" />
34+
</Grid.ColumnDefinitions>
3035

3136
<TextBlock x:Name="SelectedItemCountTextBlock"
37+
Grid.ColumnSpan="2"
3238
Margin="0,12"
3339
HorizontalAlignment="Center"
3440
FontFamily="Segoe UI Light"
3541
FontSize="21"
3642
Text="You haven't selected any items"
3743
Visibility="{Binding IsItemClickEnabled.Value, Converter={StaticResource BoolToVisibilityConverter}}" />
3844

39-
<Grid x:Name="XamlRoot" Grid.Row="1" />
45+
<Grid x:Name="XamlRoot"
46+
Grid.Row="1"
47+
Grid.ColumnSpan="2" />
48+
49+
<TextBlock Grid.Row="2"
50+
Margin="4"
51+
FontFamily="Segoe UI Light"
52+
FontSize="21"
53+
Text="Number of Photos" />
54+
55+
<Slider x:Name="NumberSlider"
56+
Grid.Row="2"
57+
Grid.Column="1"
58+
Margin="32,4"
59+
StepFrequency="1" />
4060
</Grid>
4161
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/AdaptiveGridView/AdaptiveGridViewPage.xaml.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.ObjectModel;
67
using System.Linq;
78
using Microsoft.Toolkit.Uwp.SampleApp.Data;
89
using Microsoft.Toolkit.Uwp.UI.Controls;
910
using Microsoft.Toolkit.Uwp.UI.Extensions;
1011
using Windows.UI.Popups;
1112
using Windows.UI.Xaml;
13+
using Windows.UI.Xaml.Controls.Primitives;
1214

1315
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
1416
{
1517
public sealed partial class AdaptiveGridViewPage : IXamlRenderListener
1618
{
17-
private AdaptiveGridView adaptiveGridViewControl;
19+
private AdaptiveGridView _adaptiveGridViewControl;
20+
private PhotoDataItem[] _originalPhotos;
21+
private ObservableCollection<PhotoDataItem> _boundPhotos;
1822

1923
public AdaptiveGridViewPage()
2024
{
@@ -23,19 +27,46 @@ public AdaptiveGridViewPage()
2327

2428
public async void OnXamlRendered(FrameworkElement control)
2529
{
26-
adaptiveGridViewControl = control.FindDescendantByName("AdaptiveGridViewcontrol") as AdaptiveGridView;
27-
if (adaptiveGridViewControl != null)
30+
_adaptiveGridViewControl = control.FindDescendantByName("AdaptiveGridViewcontrol") as AdaptiveGridView;
31+
if (_adaptiveGridViewControl != null)
2832
{
29-
adaptiveGridViewControl.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync();
30-
adaptiveGridViewControl.ItemClick += AdaptiveGridViewControl_ItemClick;
31-
adaptiveGridViewControl.SelectionChanged += AdaptiveGridViewControl_SelectionChanged;
33+
var allPhotos = await new Data.PhotosDataSource().GetItemsAsync();
34+
_originalPhotos = allPhotos.ToArray();
35+
_boundPhotos = new ObservableCollection<PhotoDataItem>(_originalPhotos);
36+
_adaptiveGridViewControl.ItemsSource = _boundPhotos;
37+
_adaptiveGridViewControl.ItemClick += AdaptiveGridViewControl_ItemClick;
38+
_adaptiveGridViewControl.SelectionChanged += AdaptiveGridViewControl_SelectionChanged;
39+
NumberSlider.Minimum = 1;
40+
NumberSlider.Maximum = _originalPhotos.Length;
41+
NumberSlider.Value = _originalPhotos.Length;
42+
NumberSlider.ValueChanged += OnNumberSliderValueChanged;
43+
}
44+
}
45+
46+
private void OnNumberSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
47+
{
48+
var newCount = (int)e.NewValue;
49+
var currentCount = _boundPhotos.Count;
50+
if (currentCount < newCount)
51+
{
52+
for (var i = currentCount; i < newCount; i++)
53+
{
54+
_boundPhotos.Add(_originalPhotos[i]);
55+
}
56+
}
57+
else if (currentCount > newCount)
58+
{
59+
for (var i = currentCount; i > newCount; i--)
60+
{
61+
_boundPhotos.Remove(_originalPhotos[i-1]);
62+
}
3263
}
3364
}
3465

3566
private void AdaptiveGridViewControl_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
3667
{
37-
SelectedItemCountTextBlock.Text = adaptiveGridViewControl.SelectedItems.Any()
38-
? $"You have selected {adaptiveGridViewControl.SelectedItems.Count} items."
68+
SelectedItemCountTextBlock.Text = _adaptiveGridViewControl.SelectedItems.Any()
69+
? $"You have selected {_adaptiveGridViewControl.SelectedItems.Count} items."
3970
: "You haven't selected any items";
4071
}
4172

0 commit comments

Comments
 (0)