0

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>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
thomas1234
  • 599
  • 3
  • 6
  • 15
  • Are they getting fired just when the window is loaded or constantly? – Mark Oct 15 '10 at 20:23
  • Its when Window is loaded. I can't get pass window loading because of the constant calls – thomas1234 Oct 15 '10 at 20:30
  • What's inside the grid? Are you changing the size of anything in the sizechanged event creating a loop? – mdm20 Oct 15 '10 at 20:40
  • inside the grid is a canvas. I am changing the size of the canvas to the width and height of the grid container. But I don't see why this is called more than once during startup. – thomas1234 Oct 15 '10 at 20:43
  • Can you post the XAML and the code that changes the canvas size? – mdm20 Oct 15 '10 at 20:59

1 Answers1

1

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
mdm20
  • 4,475
  • 2
  • 22
  • 24