Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF custom panel scaling to fit window
    primarykey
    data
    text
    <p>I have a WPF custom panel which arranges its child elements in a spiral shape as per my requirements. The problem I am having is to do with scaling of the items when the window is resized - at the moment it does not scale. Can anyone provide a solution? Thanks - Ben</p> <p><strong>Custom panel</strong></p> <pre><code>public class TagPanel : Panel { protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize) { Size resultSize = new Size(0, 0); foreach (UIElement child in Children) { child.Measure(availableSize); resultSize.Width = Math.Max(resultSize.Width, child.DesiredSize.Width); resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height); } resultSize.Width = double.IsPositiveInfinity(availableSize.Width) ? resultSize.Width : availableSize.Width; resultSize.Height = double.IsPositiveInfinity(availableSize.Height) ? resultSize.Height : availableSize.Height; return resultSize; } protected class InnerPos { public UIElement Element { get; set; } public Size Size { get; set; } } private Point GetSpiralPosition(double theta, Size windowSize) { double a = 5.0; double n = 1.0; double r = a * (Math.Pow(theta, 1.0 / n)); double x = r * Math.Cos(theta); double y = r * Math.Sin(theta); x += windowSize.Width / 2.0; y += windowSize.Height / 2.0; return new Point(x,y); } private Rect CreateRectangleCenteredAtPoint(Point pt, double width, double height) { return new Rect(new Point(pt.X - (width / 2.0), pt.Y - (height / 2.0)), new Size(width, height)); } protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize) { //double startPos = 0.0; List&lt;InnerPos&gt; positions = new List&lt;InnerPos&gt;(); foreach (UIElement ch in Children) { // If this is the first time // we've seen this child, add our transforms //if (ch.RenderTransform as TransformGroup == null) //{ // ch.RenderTransformOrigin = new Point(0, 0.5); // TransformGroup group = new TransformGroup(); // ch.RenderTransform = group; // group.Children.Add(new ScaleTransform()); // group.Children.Add(new TranslateTransform()); //} positions.Add(new InnerPos() { Element = ch, Size = ch.DesiredSize }); } //double currentTopMax List&lt;Rect&gt; alreadyUsedPositions = new List&lt;Rect&gt;(); foreach (InnerPos child in positions.OrderByDescending(i =&gt; i.Size.Width)) { for (double theta = 0.0; theta &lt; 100.0; theta += 0.1) { Point spiralPos = GetSpiralPosition(theta, finalSize); Rect centeredRect = CreateRectangleCenteredAtPoint(spiralPos, child.Element.DesiredSize.Width, child.Element.DesiredSize.Height); bool posIsOk = true; foreach (Rect existing in alreadyUsedPositions) { bool positionClashes = existing.IntersectsWith(centeredRect); if (positionClashes == true) { posIsOk = false; break; } } if (posIsOk) { alreadyUsedPositions.Add(centeredRect); child.Element.Arrange(centeredRect); break; } } } return finalSize; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload