wpf ScrollViewer 和 SizeChanged 事件

发布于 2024-09-27 20:19:45 字数 1370 浏览 7 评论 0原文

谁能向我解释一下 ScrollViewerSizeChanged 事件之间的关系?每当我在网格周围放置一个滚动查看器时,就会触发许多 SizeChanged 事件。两者之间是什么关系?多谢。

编辑:

从 mdm20 的评论中,我注意到如果我将网格包裹在 ScrollViewer 周围,网格的 ActualWidth 和 ActualHeight 会不断增加。谁能解释为什么会这样?我是否需要为网格的宽度和高度提供硬值?

编辑#2:

调整大小是通过下面发布的代码完成的。感谢您查看此

    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();
    }

相应的 XAML 代码如下:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2024-10-04 20:19:45

你正在陷入一个循环。我认为发生的情况是,当您更改画布大小时,它会提示网格执行布局传递,这会导致 ScrollViewer 执行布局传递,从而导致网格调整自身大小,这再次开始循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文