@@ -48,55 +48,16 @@ private void RecalculateLayout(double containerWidth)
4848 return ;
4949 }
5050
51- if ( _columns == 0 )
52- {
53- _columns = CalculateColumns ( containerWidth , DesiredWidth ) ;
54- }
55- else
56- {
57- var desiredColumns = CalculateColumns ( containerWidth , DesiredWidth ) ;
58- if ( desiredColumns != _columns )
59- {
60- _columns = desiredColumns ;
61- }
62- }
51+ _columns = CalculateColumns ( containerWidth , DesiredWidth ) ;
6352
64- // Determine the amount of items
6553 // If there's less items than there's columns,
66- // fix the ItemWidth to DesiredWidth ;
67- int count = 0 ;
68- if ( _listView != null )
54+ // reduce the column count ;
55+ if ( _listView != null && _listView . Items != null
56+ && _listView . Items . Count > 0 && _listView . Items . Count < _columns )
6957 {
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 ;
58+ _columns = _listView . Items . Count ;
9959 }
60+ ItemWidth = ( containerWidth / _columns ) - 5 ;
10061 }
10162
10263 /// <summary>
@@ -111,6 +72,7 @@ protected override void OnApplyTemplate()
11172 {
11273 _listView . SizeChanged -= ListView_SizeChanged ;
11374 _listView . ItemClick -= ListView_ItemClick ;
75+ _listView . Items . VectorChanged -= ListViewItems_VectorChanged ;
11476 _listView = null ;
11577 }
11678
@@ -119,12 +81,22 @@ protected override void OnApplyTemplate()
11981 {
12082 _listView . SizeChanged += ListView_SizeChanged ;
12183 _listView . ItemClick += ListView_ItemClick ;
84+ _listView . Items . VectorChanged += ListViewItems_VectorChanged ;
12285 }
12386
12487 _isInitialized = true ;
12588 OnOneRowModeEnabledChanged ( this , OneRowModeEnabled ) ;
12689 }
12790
91+ private void ListViewItems_VectorChanged ( Windows . Foundation . Collections . IObservableVector < object > sender , Windows . Foundation . Collections . IVectorChangedEventArgs @event )
92+ {
93+ if ( _listView != null && ! double . IsNaN ( _listView . ActualWidth ) )
94+ {
95+ // If the items count changes, check if more or less columns needs to be rendered, in case we were hitting a min-limit.
96+ RecalculateLayout ( _listView . ActualWidth ) ;
97+ }
98+ }
99+
128100 private void ListView_ItemClick ( object sender , ItemClickEventArgs e )
129101 {
130102 var cmd = ItemClickCommand ;
0 commit comments