wpf ScrollViewer 和 SizeChanged 事件
谁能向我解释一下 ScrollViewer
和 SizeChanged
事件之间的关系?每当我在网格周围放置一个滚动查看器时,就会触发许多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正在陷入一个循环。我认为发生的情况是,当您更改画布大小时,它会提示网格执行布局传递,这会导致 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.