Reputation: 508
I need to make my grid scrollable. I have a grid control that will eventually hold 30-40 rows of data. I populate/create these rows dynamically with code. Every time these rows render on the screen, they try to fit in the height I have set for the grid. I tried wrapping my grid in scrollviewer but that didn't work. I want to stay away from the listbox because text will get misaligned. Here is my XAML code for constructing the Grid:
<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>
And here is my code for creating the rows:
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);
}
Any suggestions/feedback are appreciated. Thanks!
Upvotes: 0
Views: 1247
Reputation: 16361
Using a ListBox is definitely a better idea. The problem with your code is the MaxHeight=196 on your Grid. Delete it on it will work
Upvotes: 1