In MeasureOverride (WPF Panel), how do I handle the infinity value in an accessibleSize when I don't have an inherent desired size?

I found several other questions from people who subclassed Panel in WPF, and implemented their own layout work, and got Infinity or PositiveInfinity for the dimension passed to MeasureOverride in the availableSize argument. The answers are usually "This means that you can take as much space as you want: ignore it, add the right sizes for all your children and return them."

In my case, I am making a control that dynamically resizes to fill the free space, so there is no internal concept of the desired size. He just wants to use all available size.

When I built the ControlTemplate, it turned out to be nested in the StackPanel, and it started getting Infinity for the available height, although the control did not have a scrollbar. If I just closed it with some fixed number, it just got attached to the window. I tried to return ActualHeight, but this did not work, because it is cyclical: ActualHeight is not there until I have completed the full layout. So there has always been 0.0.

At first, I did not know that the StackPanel passed Infinity and broke through the source of links for a long time, but finally I realized using VisualTreeHelper to run through my parents and see their ActualHeight and the StackPanel was the deepest with non-zero height. I changed it as a DockPanel, since I only need two controls, so it works the same as a StackPanel. And so, DockPanel does not skip Infinity for the available height.

But it just looks like a workaround; I would like to better understand the layout philosophy and why one of the standard containers without a scrollbar will tell me that I can have as much space as I want. What is the “right” answer to this?

+6
source share
3 answers

It:

I am making a control that dynamically resizes to fill the available space,

Bad combination with:

he ended up being embedded in a stackpanel

A control that fills the space should not be inside the stack panel. What size do you think should be?

The same goes for ScrollPanel.

+2
source

If you want to create an advanced StackPanel , see this blog post. This is just a simple, inflexible example, but easy enough to adapt to your scenario.

+2
source

Well, talking to other people offline, here is the best from my understanding:

In case you want to make a more complex layout using LayoutTransform or RenderTransform, thereby adding indirection between passing the control measure / organization and the actual borders of the pixels that appear inside, the message behavior of all the controls that they can use as much space as they want, reasonably, because you're just trying to execute a layout on a surface that is separate from the actual cropped area on the screen.

It still seems like it falls into the category of “if you wrap the StackPanel in something scrollable ...”, but admittedly it’s not clear that in my proposed version the available size should be after you start messing with RenderTransform for StackPanel. (Although this is more fundamental than this, RenderTransform doesn’t “play perfectly” with MeasureOverride in design because it is designed separately from the layout engine for performance ... so it’s still not clear, this is a great argument.)

In any case, the way to get the StackPanel to behave when some selected element consumes the remainder that is not used by other elements is to use the DockPanel, insert fixed size elements into the StackPanels above or below and dynamically resize either above or below or in the middle between two stacks.

I believe that if I needed several elements that consume available space in a proportional way, then what is the grid for?

But I have to say that I still do not understand, from a fundamental point of view, why the StackPanel passes Infinity down to orient its children towards the call to MeasureOverride; I see no argument for why you did not pass the actual remaining unclaimed available size passed in by the StackPanel parent, and a few arguments for why you did it.

0
source

Source: https://habr.com/ru/post/916018/


All Articles