Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomPanel : Panel
- {
- public double MinItemWidth
- {
- get { return (double)GetValue(MinItemWidthProperty); }
- set { SetValue(MinItemWidthProperty, value); }
- }
- public static readonly DependencyProperty MinItemWidthProperty =
- DependencyProperty.Register(nameof(MinItemWidth), typeof(double), typeof(CustomPanel), new PropertyMetadata(100.0, (d, e) => (d as CustomPanel)?.InvalidateMeasure()));
- protected override Size MeasureOverride(Size availableSize)
- {
- if (Children.Count > 0)
- {
- var numberOfChildrenPerRow = Math.Floor(availableSize.Width / MinItemWidth);
- var widthPerChild = availableSize.Width / numberOfChildrenPerRow;
- foreach (UIElement child in Children)
- {
- child.Measure(new Size(widthPerChild, availableSize.Height));
- availableSize.Height = Math.Max(availableSize.Height, child.DesiredSize.Height);
- }
- }
- if (Double.IsPositiveInfinity(availableSize.Height) || Double.IsPositiveInfinity(availableSize.Width))
- {
- return new Size(0, 0);
- }
- return availableSize;
- }
- protected override Size ArrangeOverride(Size finalSize)
- {
- double heightOfPreviousRows = 0;
- double heightOfCurrentRow = 0;
- if (Children.Count > 0)
- {
- var numberOfChildrenPerRow = (int)Math.Floor(finalSize.Width / MinItemWidth);
- var widthPerChild = finalSize.Width / numberOfChildrenPerRow;
- int i = 0;
- foreach (UIElement child in Children)
- {
- if (i % numberOfChildrenPerRow == 0)
- {
- heightOfPreviousRows += heightOfCurrentRow;
- heightOfCurrentRow = 0;
- }
- heightOfCurrentRow = Math.Max(heightOfCurrentRow, child.DesiredSize.Height);
- var x = (i % numberOfChildrenPerRow) * widthPerChild;
- var y = heightOfPreviousRows;
- child.Arrange(new Rect(new Point(x, y), new Size(widthPerChild, child.DesiredSize.Height)));
- ++i;
- }
- }
- return finalSize;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement