How to set the height of a Data Grid in Silverlight 4
I have a couple of DataGrids inside a page and each DataGrid is positioned via the following layout/markup:
<border BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock x:Name="Title" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="6,0,0,0" Content="Panel Title"/>
<toolkit:BusyIndicator Grid.Row="1" Grid.Column="0" x:Name="GridLoadingIndicator">
<StackPanel Orientation="Vertical">
<sdk:DataGrid x:name="GVData"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
AutoGenerateColumns="False"
HorizontalScrollBarVisibility="Visible"
SelectionMode="Single">
<datagrid:DataGridTextColumn Header="Column 1" Binding="{Binding Col1}" />
<datagrid:DataGridTextColumn Header="Column 2" Binding="{Binding Col2}" />
<datagrid:DataGridTextColumn Header="Column 3" Binding="{Binding Col3}" />
<开发者_如何学C;datagrid:DataGridTextColumn Header="Column 4" Binding="{Binding Col4}" />
<datagrid:DataGridTextColumn Header="Column 5" Binding="{Binding Col5}" />
</sdk:DataGrid>
</StackPanel>
</toolkit:BusyIndicator>
<StackPanel x:Name="PagerControls" Grid.Row="2" Grid.Column="0" Orientation="Horizontal">
<!-- Pager -->
</StackPanel>
</Grid>
</border>
Well, the problem is that the grid itself doesn't want to stretch to fill the space allotted for it, and more over it doesn't respond to page size events either.
Any ideas on how to fix the issue?
Derek Beattie is correct in his comment.
If your Grid
is within a StackPanel
and the StackPanel
does not have HorizontalAlignment="Stretch"
(i.e. in the direction that does not grow) then the stack panel will fit its children.
This voids the HorizontalAlignment="Stretch"
in your Grid
as it can only stretch to its immediate parent (the StackPanel
). The fact that it is a StackPanel
means that VerticalAlignment="Stretch"
does nothing on the inner Grid.
If you actually require the inner StackPanel
(no point with only one child), then add HorizontalAlignment="Stretch"
to it as well. That will not do anything to the vertical stretch.
Personally I would just dump the StackPanel
that surrounds the inner Grid
as it add no value.
精彩评论