Reputation: 367
I am pretty new to WPF and C#. I am trying to create a datagrid where I can programmatically set the height of the individual rows.
It seems possible to change the height for all the rows together, but I would like the rows to have different heights from each other.
Does anyone know a way to accomplish this? (I was thinking I could set the row height to automatic, and put an invisible TextBox in an unused column. I could change the height of the unused TextBox programmatically.)
Upvotes: 2
Views: 3653
Reputation: 49
Use a DataTrigger. The code below switches between Height="Auto" and Height="*". It is important that the Height is not set in the RowDefinition but in the style.
<RowDefinition>
<RowDefinition.Style>
<Style TargetType="RowDefinition">
<Setter Property="Height" Value="Auto" />
<Style.Triggers>
<DataTrigger Binding="{Binding MyFlag}" Value="True">
<Setter Property="Height" Value="*" />
</DataTrigger>
</Style.Triggers>
</Style>
</RowDefinition.Style>
Upvotes: 0
Reputation: 367
EKrueger,
Thank you very much for your response.
Your technique however, only happens when the datagrid is created. I need something that I can call at different times so I can resize my rows on a regular basis.
I kept searching and here is the solution I found.
Cheers.
Matt
private void resizeDataGridRowHeight() {
int a = boundDataGrid.Items.Count;
int calibrationRowHeight = 28;
for (int i = 0; i < a; i++) {
myRowHeight = ListofObjectsThatEachRepresentAParameter.ListOfDataTableRows[i].ListOfCalibrationRows.Count * calibrationRowHeight;
DataGridRow row = boundDataGrid.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
row = boundDataGrid.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
row.Height = myRowHeight;
}
}
Upvotes: 1
Reputation: 760
The easiest thing is probably to handle this in the DataGrid.LoadingRow event which is raised right after a row is instatiated.
To do so just add an event handler to your datagrid in the xaml code:
<DataGrid LoadingRow="DataGrid_LoadingRow"></DataGrid>
And declare this eventhandler in your code to manage the row height individually via the Height
property:
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
DataGridRow row = e.Row;
row.Height = 50; //put your height here
}
Upvotes: 3