2

I have found this question here: How to populate a WPF grid based on a 2-dimensional array and I wanted to use that the marked solution. And it also seems to work quite nice but there is one problem and that problem is that I am using ellipses to display my data. So every item equals one ellipse.

And here is the problem: The ellipses are requesting on Measure only a size of x = 0, y = 0. So my problem is that there is displayed nothing. So I tried to create a custom Stackpanel:

public class StretchStackPanel : StackPanel
{
    protected override Size MeasureOverride(Size constraint)
    {
        Size result = constraint;

        result.Width = result.Width == Double.PositiveInfinity ? Double.MaxValue : result.Width;
        result.Height = result.Height == Double.PositiveInfinity ? Double.MaxValue : result.Height;

        if (this.Orientation == System.Windows.Controls.Orientation.Horizontal)
            result.Height = result.Width / InternalChildren.Count;


        return result;
    }

    protected override Size ArrangeOverride(Size arrangeSize)
    {
        var size = base.ArrangeOverride(arrangeSize);

        double elementLength;
        if (Orientation == System.Windows.Controls.Orientation.Horizontal)
            elementLength = size.Width / InternalChildren.Count;
        else
            elementLength = size.Height / InternalChildren.Count;

        double current = 0;
        foreach (UIElement e in InternalChildren)
        {
            Rect result;
            if (Orientation == System.Windows.Controls.Orientation.Horizontal)
            {
                result = new Rect(current, 0, elementLength, size.Height);
                current += result.Width;
            }
            else
            {
                result = new Rect(0, current, size.Width, elementLength);
                current += result.Height;
            }

            e.Arrange(result);
        }

        return size;
    }
}

But there is a very strange behaviour. There are very very smalle margins between the items and the seem to be random. If I resize you can see them moving.

here are two screenshots:

enter image description here enter image description here

I also tried to use a Uniformgrid with columns only instead of Stackpanel and set an itemstyle with a fixed height using a ViewBox around the Itemscontrol.

Community
  • 1
  • 1
Florian
  • 5,918
  • 3
  • 47
  • 86

0 Answers0