Click here to Skip to main content
15,662,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having an issue with having a scrollviewer size itself.

I have the following xaml:

<Grid >
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />

    <Grid Grid.Row="0" Grid.Column="0">
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />

        <!-- record number panel -->
        <Border x:Name="borderLeftColHeader" Grid.Row="0" >
            <!-- content stuff here -->

        <!-- QUESTIONS LIST -->
        <Grid Grid.Row="1" x:Name="gridScrollView" Margin="0,0,5,0" >
            <ScrollViewer x:Name="svQuestions" VerticalScrollBarVisibility="Auto" 
                          Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" >
                <!-- this grid is filled in code-behind when the 
                parent user control is instantiated - can have 1-n 
                children -->
                <Grid x:Name="gridQuestions" />

        <!-- QUESTION BUTTONS ROW 1 -->
        <Grid Grid.Row="2" Margin="0,0,5,0" >
            <!-- content stuff here -->
        <!-- QUESTION BUTTONS ROW 2 -->
        <Grid Grid.Row="3" Margin="0,5,0,0">
            <!-- content stuff here -->

My problem is that if I specifically set the height of the scrollviewer, it displays as expected.

If I used the binding indicated above (Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}), it uses the ActualHeight of its parent grid which is always 0.

I also tried binding to the grid's element name (using the same Path=ActualHeight).

I also tried adding SizeChanged and UpdatedLayout event handlers to the parent user control and all related elements so I could inspect the ActualHeight of the parent grid (gridScrollView) in the debugger, and it's always 0.

I'm out of ideas. What do I need to do to ensure that the gridScrollView.ActualHeight gets set to something useful, preferably when the user control gets instantiated?
Updated 16-Sep-15 5:20am

1 solution

Never mind. I found the problem. The grid containing this mess had a set of row definitions that weren't needed (and the row I was using was set to "Auto".
Share this answer
Member 15634627 13-Jan-23 5:45am    
Can u provide the code, how u fixed it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900