44
55using System ;
66using System . Collections . Generic ;
7- using System . Linq ;
87using Windows . Foundation ;
98using Windows . UI . Xaml ;
109using Windows . UI . Xaml . Controls ;
@@ -72,7 +71,7 @@ public VerticalAlignment VerticalContentAlignment
7271 nameof ( VerticalContentAlignment ) ,
7372 typeof ( VerticalAlignment ) ,
7473 typeof ( WrapPanel ) ,
75- new PropertyMetadata ( VerticalAlignment . Top ) ) ;
74+ new PropertyMetadata ( VerticalAlignment . Top , LayoutPropertyChanged ) ) ;
7675
7776 /// <summary>
7877 /// Gets or sets the orientation of the WrapPanel.
@@ -219,6 +218,7 @@ protected override Size MeasureOverride(Size availableSize)
219218 /// <inheritdoc />
220219 protected override Size ArrangeOverride ( Size finalSize )
221220 {
221+ var rows = new List < Row > ( ) ;
222222 if ( Children . Count > 0 )
223223 {
224224 var parentMeasure = new UvMeasure ( Orientation , finalSize . Width , finalSize . Height ) ;
@@ -227,14 +227,10 @@ protected override Size ArrangeOverride(Size finalSize)
227227 var paddingEnd = new UvMeasure ( Orientation , Padding . Right , Padding . Bottom ) ;
228228 var position = new UvMeasure ( Orientation , Padding . Left , Padding . Top ) ;
229229
230- var allMeasures = Children . Select ( c => new UvMeasure ( Orientation , c . DesiredSize ) ) . ToList ( ) ;
231- var allData = new List < ( UvMeasure position , UvMeasure size ) > ( ) ;
232-
233- var currentV = 0.0 ;
234- void Arrange ( int childIndex , bool isLast = false )
230+ var currentRow = new Row ( ) ;
231+ void Arrange ( UIElement child , bool isLast = false )
235232 {
236- var child = Children [ childIndex ] ;
237- var desiredMeasure = allMeasures [ childIndex ] ;
233+ var desiredMeasure = new UvMeasure ( Orientation , child . DesiredSize ) ;
238234 if ( desiredMeasure . U == 0 )
239235 {
240236 return ; // if an item is collapsed, avoid adding the spacing
@@ -244,8 +240,10 @@ void Arrange(int childIndex, bool isLast = false)
244240 {
245241 // next row!
246242 position . U = paddingStart . U ;
247- position . V += currentV + spacingMeasure . V ;
248- currentV = 0 ;
243+ position . V += currentRow . Size . V + spacingMeasure . V ;
244+
245+ rows . Add ( currentRow ) ;
246+ currentRow = new Row ( ) ;
249247 }
250248
251249 // Stretch the last item to fill the available space
@@ -254,69 +252,45 @@ void Arrange(int childIndex, bool isLast = false)
254252 desiredMeasure . U = parentMeasure . U - position . U ;
255253 }
256254
257- allData . Add ( ( position , desiredMeasure ) ) ;
255+ currentRow . Add ( position , desiredMeasure ) ;
258256
259257 // adjust the location for the next items
260258 position . U += desiredMeasure . U + spacingMeasure . U ;
261- currentV = Math . Max ( desiredMeasure . V , currentV ) ;
262259 }
263260
264261 var lastIndex = Children . Count - 1 ;
265262 for ( var i = 0 ; i < lastIndex ; i ++ )
266263 {
267- Arrange ( i ) ;
264+ Arrange ( Children [ i ] ) ;
268265 }
269266
270- Arrange ( lastIndex , StretchChild == StretchChild . Last ) ;
267+ Arrange ( Children [ lastIndex ] , StretchChild == StretchChild . Last ) ;
268+ if ( currentRow . ChildrenRects . Count > 0 )
269+ {
270+ rows . Add ( currentRow ) ;
271+ }
272+ }
271273
274+ if ( rows . Count > 0 )
275+ {
272276 // Now that we have all the data, we do the actual arrange pass
273- var lastArrangeV = - 1.0 ;
274- var maxRowHeight = 0.0 ;
275- for ( var i = 0 ; i < Children . Count ; i ++ )
277+ var childIndex = 0 ;
278+ foreach ( var row in rows )
276279 {
277- // place the item
278- var child = Children [ i ] ;
279- ( UvMeasure arrangePosition , UvMeasure arrangeMeasure ) = allData [ i ] ;
280-
281- if ( lastArrangeV != arrangePosition . V )
282- {
283- // We are on a new row, we scan all the items to get the max row height
284- maxRowHeight = allData
285- . Skip ( i ) // ignore what has already being processed
286- . TakeWhile ( d => d . position . V == arrangePosition . V ) // we are still on the same row
287- . Max ( d => d . size . V ) ; // We want the max.
288- }
289-
290- if ( Orientation == Orientation . Horizontal )
280+ foreach ( var rect in row . ChildrenRects )
291281 {
292- switch ( VerticalContentAlignment )
282+ var child = Children [ childIndex ++ ] ;
283+ UvRect arrangeRect ;
284+ if ( Orientation == Orientation . Horizontal )
293285 {
294- case VerticalAlignment . Center :
295- {
296- var vOffset = Math . Max ( ( maxRowHeight - arrangeMeasure . V ) / 2.0 , 0.0 ) ;
297- child . Arrange ( new Rect ( arrangePosition . U , arrangePosition . V + vOffset , arrangeMeasure . U , arrangeMeasure . V ) ) ;
298- break ;
299- }
300-
301- case VerticalAlignment . Bottom :
302- {
303- var vOffset = Math . Max ( maxRowHeight - arrangeMeasure . V , 0.0 ) ;
304- child . Arrange ( new Rect ( arrangePosition . U , arrangePosition . V + vOffset , arrangeMeasure . U , arrangeMeasure . V ) ) ;
305- break ;
306- }
307-
308- case VerticalAlignment . Stretch :
309- child . Arrange ( new Rect ( arrangePosition . U , arrangePosition . V , arrangeMeasure . U , maxRowHeight ) ) ;
310- break ;
311- case VerticalAlignment . Top :
312- default :
313- child . Arrange ( new Rect ( arrangePosition . U , arrangePosition . V , arrangeMeasure . U , arrangeMeasure . V ) ) ;
314- break ;
286+ arrangeRect = rect . WithVerticalAlignment ( VerticalContentAlignment , row . Size . V ) ;
315287 }
316- }
317- else
318- {
319- child . Arrange ( new Rect ( arrangePosition . V , arrangePosition . U , arrangeMeasure . V , arrangeMeasure . U ) ) ;
288+ else
289+ {
290+ arrangeRect = rect ;
291+ }
292+
293+ child . Arrange ( arrangeRect . ToRect ( Orientation ) ) ;
320294 }
321295 }
322296 }
0 commit comments