private void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (var item in e.OldItems) { FluidElements.Remove((UIElement)item); } } if (e.NewItems != null) { foreach (var item in e.NewItems) { // Check if the child is already added to the FluidElements collection if (!FluidElements.Contains((UIElement)item)) { AddChildToFluidElements((UIElement)item); } } } }
/// <summary> /// Override for the Measure Layout Phase /// </summary> /// <param name="availableSize">Available Size</param> /// <returns>Size required by the panel</returns> protected override Size MeasureOverride(Size availableSize) { Size availableItemSize = new Size(Double.PositiveInfinity, Double.PositiveInfinity); double rowWidth = 0.0; double maxRowHeight = 0.0; double colHeight = 0.0; double maxColWidth = 0.0; double totalColumnWidth = 0.0; double totalRowHeight = 0.0; // Iterate through all the UIElements in the Children collection for (int i = 0; i < InternalChildren.Count; i++) { UIElement child = InternalChildren[i]; if (child != null) { // Ask the child how much size it needs child.Measure(availableItemSize); //Check if the child is already added to the FluidElements collection if (!FluidElements.Contains(child)) { AddChildToFluidElements(child); } if (this.Orientation == Orientation.Horizontal) { // Will the child fit in the current row? //if (rowWidth + child.DesiredSize.Width > availableSize.Width) if (rowWidth + this.ItemWidth > availableSize.Width) { // Wrap to next row totalRowHeight += maxRowHeight; // Is the current row width greater than the previous row widths if (rowWidth > totalColumnWidth) { totalColumnWidth = rowWidth; } rowWidth = 0.0; maxRowHeight = 0.0; } //rowWidth += child.DesiredSize.Width; rowWidth += this.ItemWidth; //if (child.DesiredSize.Height > maxRowHeight) // maxRowHeight = child.DesiredSize.Height; if (this.ItemHeight > maxRowHeight) { maxRowHeight = this.ItemHeight; } } else // Vertical Orientation { // Will the child fit in the current column? //if (colHeight + child.DesiredSize.Height > availableSize.Height) if (colHeight + this.ItemHeight > availableSize.Height) { // Wrap to next column totalColumnWidth += maxColWidth; // Is the current column height greater than the previous column heights if (colHeight > totalRowHeight) { totalRowHeight = colHeight; } colHeight = 0.0; maxColWidth = 0.0; } //colHeight += child.DesiredSize.Height; colHeight += this.ItemHeight; //if (child.DesiredSize.Width > maxColWidth) // maxColWidth = child.DesiredSize.Width; if (this.ItemWidth > maxColWidth) { maxColWidth = this.ItemWidth; } } } } List <UIElement> dirtyElements = new List <UIElement>(); foreach (var element in FluidElements) { if (!InternalChildren.Contains(element)) { dirtyElements.Add(element); } } foreach (var item in dirtyElements) { FluidElements.Remove(item); } if (this.Orientation == Orientation.Horizontal) { // Add the height of the last row totalRowHeight += maxRowHeight; // If there is only one row, take its width as the total width if (totalColumnWidth == 0.0) { totalColumnWidth = rowWidth; } } else { // Add the width of the last column totalColumnWidth += maxColWidth; // If there is only one column, take its height as the total height if (totalRowHeight == 0.0) { totalRowHeight = colHeight; } } Size resultSize = new Size(totalColumnWidth, totalRowHeight); return(resultSize); }