Skip to content

Commit c258bc4

Browse files
Morten NielsenMorten Nielsen
authored andcommitted
Fixed column resizing when the number of items are less than the number of columns
1 parent a938c10 commit c258bc4

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/AdaptiveGridView/AdaptiveGridView.Properties.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public sealed partial class AdaptiveGridView
6262
DependencyProperty.Register(nameof(OneRowModeEnabled), typeof(bool), typeof(AdaptiveGridView), new PropertyMetadata(false, (o, e) => { OnOneRowModeEnabledChanged(o, e.NewValue); }));
6363

6464
/// <summary>
65-
/// Identifies the <see cref="VerticalScroll"/> dependency property.
65+
/// Identifies the <see cref="VerticalScrollMode"/> dependency property.
6666
/// </summary>
67-
private static readonly DependencyProperty VerticalScrollProperty =
68-
DependencyProperty.Register(nameof(VerticalScroll), typeof(ScrollMode), typeof(AdaptiveGridView), new PropertyMetadata(ScrollMode.Auto));
67+
private static readonly DependencyProperty VerticalScrollModeProperty =
68+
DependencyProperty.Register(nameof(VerticalScrollMode), typeof(ScrollMode), typeof(AdaptiveGridView), new PropertyMetadata(ScrollMode.Auto));
6969

7070
/// <summary>
7171
/// Identifies the <see cref="ItemWidth"/> dependency property.
@@ -94,7 +94,7 @@ private static void OnOneRowModeEnabledChanged(DependencyObject d, object newVal
9494
};
9595

9696
self._listView.SetBinding(GridView.MaxHeightProperty, b);
97-
self.VerticalScroll = ScrollMode.Disabled;
97+
self.VerticalScrollMode = ScrollMode.Disabled;
9898
}
9999
}
100100
}
@@ -173,10 +173,10 @@ public bool OneRowModeEnabled
173173
/// </summary>
174174
public event ItemClickEventHandler ItemClick;
175175

176-
private ScrollMode VerticalScroll
176+
private ScrollMode VerticalScrollMode
177177
{
178-
get { return (ScrollMode)GetValue(VerticalScrollProperty); }
179-
set { SetValue(VerticalScrollProperty, value); }
178+
get { return (ScrollMode)GetValue(VerticalScrollModeProperty); }
179+
set { SetValue(VerticalScrollModeProperty, value); }
180180
}
181181

182182
private double ItemWidth

Microsoft.Toolkit.Uwp.UI.Controls/AdaptiveGridView/AdaptiveGridView.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
1010
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
1111
// ******************************************************************
12+
using System.Collections.Generic;
1213
using Windows.UI.Xaml;
1314
using Windows.UI.Xaml.Controls;
1415

@@ -60,7 +61,42 @@ private void RecalculateLayout(double containerWidth)
6061
}
6162
}
6263

63-
ItemWidth = (containerWidth / _columns) - 5;
64+
// Determine the amount of items
65+
// If there's less items than there's columns,
66+
// fix the ItemWidth to DesiredWidth;
67+
int count = 0;
68+
if (_listView != null)
69+
{
70+
count = _listView.Items.Count;
71+
}
72+
else if (ItemsSource is System.Collections.ICollection)
73+
{
74+
count = (ItemsSource as System.Collections.ICollection).Count;
75+
}
76+
else if (ItemsSource is System.Array)
77+
{
78+
count = (ItemsSource as System.Array).Length;
79+
}
80+
else if (ItemsSource is System.Collections.IEnumerable)
81+
{
82+
var enumerable = ((System.Collections.IEnumerable)ItemsSource).GetEnumerator();
83+
while (count < _columns && enumerable.MoveNext())
84+
{
85+
// All we need to know is if there's less than columns, so we'll stop when reaching _columns
86+
count++;
87+
}
88+
}
89+
90+
if (count < _columns && count != 0)
91+
{
92+
// If there aren't enough items to fill the column, set the fixed size
93+
// as the largest an item would be before introducing one less column
94+
ItemWidth = (DesiredWidth / count * (count + 1)) - 5;
95+
}
96+
else
97+
{
98+
ItemWidth = (containerWidth / _columns) - 5;
99+
}
64100
}
65101

66102
/// <summary>

Microsoft.Toolkit.Uwp.UI.Controls/AdaptiveGridView/AdaptiveGridView.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<ControlTemplate TargetType="controls:AdaptiveGridView">
99
<Grid>
1010
<ContentPresenter x:Name="templateProxy" Width="{TemplateBinding ItemWidth}" Height="{TemplateBinding ItemHeight}" ContentTemplate="{TemplateBinding ItemTemplate}" Visibility="Collapsed" />
11-
<GridView x:Name="ListView" IsItemClickEnabled="True" SelectionMode="None" IsSwipeEnabled="False" IsTabStop="False"
12-
ScrollViewer.VerticalScrollMode="{TemplateBinding VerticalScroll}" ScrollViewer.VerticalScrollBarVisibility="Hidden"
11+
<GridView HorizontalAlignment="Stretch" x:Name="ListView" IsItemClickEnabled="True" SelectionMode="None" IsSwipeEnabled="False" IsTabStop="False"
12+
ScrollViewer.VerticalScrollMode="{TemplateBinding VerticalScrollMode}" ScrollViewer.VerticalScrollBarVisibility="Hidden"
1313
ItemsSource="{TemplateBinding ItemsSource}">
1414
<GridView.ItemTemplate>
1515
<DataTemplate>

0 commit comments

Comments
 (0)