/ Published in: C#
This is a flowpanel that automatically justifies it's lines so that the elements are justified.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; namespace JustifyingWrapPanelTest { public class StretchWrapPanel : Panel { protected override Size MeasureOverride(Size availableSize) { double lineWidth = 0; double lineHeight = 0; double totalWidth = 0; double totalHeight = 0; foreach (UIElement element in this.Children) { element.Measure(asBigAsYouWant); Size itemSize = GetItemDesiredSize(element); // reset the line if we run out of room if ((this.Children[this.Children.Count - 1].Equals(element)) || (lineWidth + GetItemDesiredSize(this.Children[this.Children.IndexOf(element) + 1]).Width > availableSize.Width)) { if (lineWidth > totalWidth) totalWidth = lineWidth; lineWidth = 0; totalHeight += lineHeight; lineHeight = 0; } lineWidth += itemSize.Width; // take the tallest item in the line as our lineHeight if (itemSize.Height > lineHeight) lineHeight = itemSize.Height; } } protected override Size ArrangeOverride(Size finalSize) { int lineCount = 0; double lineWidth = 0; double lineHeight = 0; double top = 0; double left = 0; double width = 0; double height = 0; double gap = 0; for (int u = 0; u <= this.Children.Count - 1; u++) { UIElement curr = this.Children[u]; Size currSize = GetItemDesiredSize(curr); lineCount++; lineWidth = lineWidth + currSize.Width; lineHeight = Math.Max(lineHeight, currSize.Height); bool isLast = this.Children[this.Children.Count - 1].Equals(curr); if (isLast || (lineWidth + GetItemDesiredSize(this.Children[u + 1]).Width) >= finalSize.Width) { gap = isLast ? gap : lineCount <= 1 ? 0 : (finalSize.Width - lineWidth) / (lineCount - 1); for (int i = (u - lineCount) + 1; i <= u; i++) { UIElement element = this.Children[i]; width = GetItemDesiredSize(element).Width; height = GetItemDesiredSize(element).Height; left += (width + gap); } top += lineHeight; left = 0; // reset our line markers lineWidth = 0; lineHeight = 0; lineCount = 0; } } return finalSize; } private Size GetItemDesiredSize(UIElement element) { itemSize.Width = double.IsInfinity(element.DesiredSize.Width) ? 0 : element.DesiredSize.Width; itemSize.Height = double.IsInfinity(element.DesiredSize.Height) ? 0 : element.DesiredSize.Height; return itemSize; } } }