private static void SeedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { if (obj is RandomPanel) { RandomPanel owner = (RandomPanel)obj; owner._random = new Random((int)args.NewValue); owner.InvalidateArrange(); } }
private static void OnInvalidateMeasure(DependencyObject d, DependencyPropertyChangedEventArgs e) { RandomPanel panel = d as RandomPanel; if (panel.MinimumWidth > panel.MaximumWidth) { throw new InvalidDataException("MinWidth can't be greater than MaxWidth."); } else if (panel.MinimumHeight > panel.MaximumHeight) { throw new InvalidDataException("MinHeight can't be greater than MaxHeight."); } else { ((AnimationPanel)d).InvalidateMeasure(); } }
protected override Size MeasureChildrenOverride(UIElementCollection children, Size constraint) { Size availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity); foreach (UIElement child in children) { if (child == null) { continue; } Size childSize = new Size(1d * _random.Next(Convert.ToInt32(MinimumWidth), Convert.ToInt32(MaximumWidth)), 1d * _random.Next(Convert.ToInt32(MinimumHeight), Convert.ToInt32(MaximumHeight))); child.Measure(childSize); RandomPanel.SetActualSize(child, childSize); } return(new Size()); }
protected override Size ArrangeChildrenOverride(UIElementCollection children, Size finalSize) { foreach (UIElement child in children) { if (child == null) { continue; } Size childSize = RandomPanel.GetActualSize(child); double x = _random.Next(0, (int)(Math.Max(finalSize.Width - childSize.Width, 0))); double y = _random.Next(0, (int)(Math.Max(finalSize.Height - childSize.Height, 0))); double width = Math.Min(finalSize.Width, childSize.Width); double height = Math.Min(finalSize.Height, childSize.Height); this.ArrangeChild(child, new Rect(new Point(x, y), new Size(width, height))); } return(finalSize); }