Rock3rRullz
Rock3rRullz

Reputation: 465

Panel ArrangeOverride & MeasureOverride Methods

Can someone provide a simple example for this functions? What i`m trying to realize is something like this:

When i have 1 child - i want to be displayed on full screen;

When i have 2 children - i want to have the same height and width and be displayed each one on a full row;

When i have 3 children, something like this:

|*************|
|   1  |   2  |
|*************|
|      3      |
|*************|

And when i have 4 children to be:

|*************|
|   1  |   2  |
|*************|
|   3  |   4  |
|*************|

How can i achieve this?

Regards.

EDIT 1: I think this panel can inherit the UniformGrid somwhow.

Upvotes: 3

Views: 2516

Answers (1)

EgorBo
EgorBo

Reputation: 6142

It seems that you don't need to override MeasureOverride because your panel will take all available space, so you just need to implement your placing logic in ArrangeOverride something like this:

public class CustomPanel : Panel
{
    protected override Size ArrangeOverride(Size finalSize)
    {
        if (Children.Count == 1)
        {
            Children[0].Arrange(new Rect(new Point(0, 0), finalSize));
        }
        else if (Children.Count == 2)
        {
            var halfFinalSize = new Size(finalSize.Width, finalSize.Height/2);
            Children[0].Arrange(new Rect(new Point(0, 0), halfFinalSize));
            Children[1].Arrange(new Rect(new Point(0, finalSize.Height / 2), halfFinalSize));
        }
        else if (Children.Count == 3)
        {
            var halfFinalSize = new Size(finalSize.Width, finalSize.Height / 2);
            var quarterSize = new Size(finalSize.Width / 2, finalSize.Height / 2);

            Children[0].Arrange(new Rect(new Point(0, 0), halfFinalSize));
            Children[1].Arrange(new Rect(new Point(0, finalSize.Height / 2), quarterSize));
            Children[2].Arrange(new Rect(new Point(finalSize.Width / 2, finalSize.Height / 2), quarterSize));
        }
        else if (Children.Count == 4)
        {
            var quarterSize = new Size(finalSize.Width / 2, finalSize.Height / 2);
            Children[0].Arrange(new Rect(new Point(0, 0), quarterSize));
            Children[1].Arrange(new Rect(new Point(finalSize.Width / 2, 0), quarterSize));
            Children[2].Arrange(new Rect(new Point(0, finalSize.Height / 2), quarterSize));
            Children[3].Arrange(new Rect(new Point(finalSize.Width / 2, finalSize.Height / 2), quarterSize));
        }
        else if (Children.Count > 4)
        {
            //???
        }

        return base.ArrangeOverride(finalSize);
    }
}`

PS: make sure that Children count is less than 5

Upvotes: 8

Related Questions