Question

How do I get the width available for the children of a scroll viewer in XAML? Thanks.

Was it helpful?

Solution

There's no direct way of doing this that I know of, since WPF automatically passes the available space in to the child controls' Measure() function so that they size to fit the available space.

Note that, by default, it passes in infinity for the vertical direction, since content can scroll forever vertically. You can change the visibility of the scroll bars in both the vertical and horizontal direction to affect whether infinity is passed vertically, horizontally, or both.

The best way of figuring out how wide the child controls actually have to layout in pure XAML would be to create an empty control - for instance, an empty grid - and then bind to its ActualWidth property:

<ScrollViewer>
    <StackPanel>
        <Grid x:Name="MeasureGrid"/>
        <TextBox Text="{Binding ElementName=MeasureGrid, Path=ActualWidth}"/>
    </StackPanel>
</ScrollViewer>

Aside from displaying the width that is actually available to controls, I don't see any other use for this information in XAML, though - all of the other scenarios I can think of can use this information implicitly. Can you give us more information on what you are trying to accomplish?

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