Reputation: 2142
I am developing a WPF application. In that i have a Grid
which has two rows. One row consist of datagrid
and other row has some TextBlock
to display the detailed view of selected item
in DataGrid
.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Name="datagrid" Height="8*"></RowDefinition>
<RowDefinition Name="detailedview" Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<my:DataGrid
Grid.Row="0"
x:Name="dataGrid1"
Width="auto"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
AutoGenerateColumns="False" CanUserAddRows="True" Margin="0,0,0,0">
<Grid Grid.Row="2">
<!- Detailed View->
</Grid>
</Grid>
Problem is , I am assigning the ItemSource of the Datagrid to an IEnumerable collection in my code behind.
Row size of the Grid should have the size equal to the number of rows in the datagrid exactly.
Remaining space should be occupied by Detailed view.
When 2 or more rows are added. the height of the row which is having datagrid should expand as such it should accomadate exactly 2 rows and size of detailed view should decrease relatively.
How can I acheive it without harcoding it into 8* and 2*.?
Upvotes: 0
Views: 989
Reputation: 3261
You can put you DataGrid into ScrollViewer. Try next markup:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0">
<my:DataGrid ... />
</ScrollViewer>
<TextBlock Height="40" Grid.Row="1" />
</Grid>
Upvotes: 0
Reputation: 4109
For the datagrid row set Height to Auto and for the deatiled row set height to * as shown below.
<RowDefinition Name="datagrid" Height="Auto"></RowDefinition>
<RowDefinition Name="detailedview" Height="*"></RowDefinition>
Since the datagrid row height is Auto, set the MinHeight property of the Datagrid to some value so that the datagrid occupies more screen space even if there are no records to display. Below I have set the MinHeight to 80.
<my:DataGrid
MinHeight="80"
Grid.Row="0"
x:Name="dataGrid1"
Width="auto"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
AutoGenerateColumns="False" CanUserAddRows="True" Margin="0,0,0,0">
You may also want to set a minimum height to the TextBlock so that it doesn't completely shrink when there are more rows in the datagrid.
Upvotes: 2