WPF what determines whether scrollviewer and stackpanel give infinite size to a child as opposed to a set size?

StackOverflow https://stackoverflow.com/questions/17708021

  •  03-06-2022
  •  | 
  •  

Question

During Measure, I'm still really confused because this results in stackpanel's child (signalgraph) being given (292,Infinity) as the availableSize in Measure:

<Window x:Class="paneltesting.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpfExp="clr-namespace:WpfExperimental"  
    Title="Window1" Height="300" Width="300">

    <StackPanel
    >
      <wpfExp:SignalGraph/>
  </StackPanel>
</Window>

While with a canvas of a set size the child is given (infinity, infinity) for its available size. I assume that this has something to do with an interaction with the window, but I'm really confused as to what is going on.

<Canvas Width="100" Height="100">
    <StackPanel SizeChanged="ScrollViewer_SizeChanged"
    >
      <wpfExp:SignalGraph/>
    </StackPanel>
</Canvas>

The same thing occurs when a scrollviewer is used. I just wanted to be able to understand how scrollviewer gives sizes to its children. I was wondering if changing the panels in its controltemplate might result in a scrollviewer that just passes the scrollviewer size as the availableSize to its child instead of infinite space.

I've already coded a work around, but I just want to understand this, so that I'm certain that there wasn't some much easier way to get the behavior I wanted.

Was it helpful?

Solution

StackPanel measures its children with infinity in the direction of its Orientation, so an infinite height when the Orientation is Vertical; the other orientation is usually given the constraint it got.

Canvas always measures its children with an infinite width and height.

For a ScrollViewer, if the Content implements IScrollInfo (i.e. it provides the scrolling functionality) and its CanContentScroll is true then the child is measured based on the constraint it is given, possibly minus the amount needed for the scrollbar(s) if they are shown. Of course then it is responsible for handling the scrolling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top