Question

Can anyone explain to me the relationship between ScrollViewer and SizeChanged event? Whenever I put a scrollViewer around a grid, numerous SizeChanged event gets fired. What is the relationship between the two? Thanks a lot.

EDIT:

From mdm20's comment, I noticed that the ActualWidth and ActualHeight of the grid increases continuously if I wrap the grid around a ScrollViewer. Can anyone explain why this is the case? Do I need to have hard values for the width and height of the grid?

EDIT #2:

The resizing is done through code posted below. Thanks for looking into this

    private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        textCanvas.Width = chartGrid.ActualWidth;
        textCanvas.Height = chartGrid.ActualHeight;
        legendCanvas.Children.Clear();
        chartCanvas.Children.RemoveRange(1, chartCanvas.Children.Count - 1);
        textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);
        AddChart();
    }

Corresponding XAML code is below:

<ScrollViewer Name="chartScrollViewer">
        <Grid Margin="0" x:Name ="chartGrid" Grid.Column="1" Grid.Row="1" ClipToBounds="True" Background="Transparent" SizeChanged="chartGrid_SizeChanged">
            <Canvas Margin="2" Name="textCanvas" ClipToBounds="True" Grid.Column="1" Grid.Row="1" Height="1200">
                <Canvas Name="chartCanvas" ClipToBounds="True">
                    <Canvas Name="legendCanvas" Background="Transparent" />
                </Canvas>
            </Canvas>
        </Grid>
</ScrollViewer>
Was it helpful?

Solution

You are getting into a loop. I think what is happening is that when you change the canvas size, it prompts the grid to do a layout pass, which causes the ScrollViewer to do a layout pass, which causes the grid to resize itself, which starts the cycle over again.

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