我需要让我的网格可滚动。我有一个网格控件,最终将容纳30-40行数据。我用代码动态填充/创建这些行。每次这些行在屏幕上呈现时,它们都会尝试适合我为网格设置的高度。我尝试在scrollviewer中包装我的网格,但这不起作用。我想远离列表框,因为文本会不对齐。这是我用于构建Grid的XAML代码:
<ScrollViewer Canvas.Left="56" Canvas.Top="354">
<Grid x:Name="grdWeather" Canvas.Left="56" Canvas.Top="354" Width="371" ShowGridLines="True" Background="#FFDCB3B3" ScrollViewer.VerticalScrollBarVisibility="Visible" MaxHeight="196">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
</Grid>
</ScrollViewer>
这是我创建行的代码:
for (int t = 0; t <= 30; t++)
{
RowDefinition row1 = new RowDefinition();
grdWeather.RowDefinitions.Add(row1);
TextBlock txtTime = new TextBlock();
txtTime.Text = time;
txtTime.SetValue(Grid.RowProperty, t);
txtTime.SetValue(Grid.ColumnProperty, 0);
txtTime.Width = 100;
grdWeather.Children.Add(txtTime);
TextBlock txtTemp = new TextBlock();
txtTemp.Text = time;
txtTemp.Width = 100;
txtTemp.SetValue(Grid.RowProperty, t);
txtTemp.SetValue(Grid.ColumnProperty, 1);
grdWeather.Children.Add(txtTemp);
}
任何建议/反馈都表示赞赏。谢谢!
答案 0 :(得分:1)
使用ListBox绝对是一个更好的主意。代码的问题是网格上的MaxHeight = 196。将其删除就可以了