C# – Expand row in WPF datagrid

cwpfwpfdatagrid

I am using a DataGrid to display some log files, where each cell contains a TextBlock. I need help creating a method to expand a user selected row like this:

http://dl.dropbox.com/u/5649690/StackOverflow%20-%20Do%20not%20delete/Expand%20row%20in%20wpf%20datagrid/Do%20want.png

This is my code right now. It is based on the index of the clicked row:

DataGridRow testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);

logBrowserDataGrid.UpdateLayout();
logBrowserDataGrid.ScrollIntoView(logBrowserDataGrid.Items[index]);

testrow = (DataGridRow)logBrowserDataGrid.ItemContainerGenerator.ContainerFromIndex(index);
testrow.Height = 100;

However this creates a weird result:

http://dl.dropbox.com/u/5649690/StackOverflow%20-%20Do%20not%20delete/Expand%20row%20in%20wpf%20datagrid/Do%20not%20want%20.png

Do you know a god way to expand a row based on the index?

Do you know what happens in the weird result i get? It looks like i am expanding a part of the row, and the rest does stretch out. I have also studied it in runtime, and can see that its height is the correct 100, but the ActuallyHeight is still 20.

Additional info:
The default size of the rows are set by the .RowHeight property on the DataGrid.
I am using the AutoGenerateColumns feature, plus catching the AutogeneratingColumn event to replace the column with a DataGridTemplateColumn.

Best Answer

Why not replace the default DataGridCellTemplate with an Expander to do all that for you?

<DataGridColumn>
    <DataGridColumn.CellTemplate>
        <DataTemplate>
            <Expander Header="{Binding SomeText}">
                <TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
            </Expander>
        </DataTemplate>
    </DataGridColumn.CellTemplate>
</DataGridColum>

If you don't like the default Expander look, you can overwrite it's Template to look like a plain TextBlock

As a side note, to stretch and vertically align a DataGridRow, you want to stretch and align the Cell Content, not the Row.