Hierarchical/nested datagrids row details layout problem

Feb 20, 2009 at 7:49 AM
When I nest datagrids so that I add new datagrid to other grids RowDetailsTemplate, I would expect the layout of the outer grid dynamically change when the dimensions of the inner grids change. However, this seems not to be the case in all situations. The outer grid dynamically increases it's size to show it's content, but it won't decrease it's size after content's size decreases.
The following animation demonstrates the problem. When the animated rectangles increase in size the height of the outer grid increases respectively, but when the rectangles start to minimize, the outer grid doesn't minimize. Is this a bug or a feature, and how it possibly can be fixed?

<Window x:Class="Test.Window"
        <XmlDataProvider x:Key="Data" XPath="Data/Colors/Color">
                <Data xmlns="">
                        <Color Name="Red">
                            <Test />
                        <Color Name="Green">
                            <Test />
                        <Color Name="Blue">
                            <Test />
                        <Color Name="Orange">
                            <Test />
    <Controls:DataGrid AutoGenerateColumns="False"
                       ItemsSource="{Binding Source={StaticResource Data}}"
                <Controls:DataGrid DockPanel.Dock="Top"
                                   ItemsSource="{Binding XPath=Test}"
                            <Rectangle Width="50"
                                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                                                <Storyboard Storyboard.TargetProperty="Height">
                                                    <DoubleAnimationUsingKeyFrames Duration="0:0:5"
                                                        <LinearDoubleKeyFrame Value="100" />
            <Controls:DataGridTextColumn Header="Name"
                                         Binding="{Binding XPath=@Name}" />
Feb 20, 2009 at 6:30 PM
Hi annyman,

This is actually by design.  The main scenario that this feature was designed for was scrolling when virtualization is turned on, and we decided that cell sizes should be "grow only" by default.  Basically, if you're scrolling through the grid, as cells become unvirtualized, the minimum cell size will grow to fit the largest cell content.  If we then made the cells shrink as smaller content came into view, you'd see the cells oscillating their width, which can be really distracting and create a jumpy UI.  In addition, shrinking the cells back down would require a costly measure and arrange pass, so we did this as a performance enhancement also. 

We've gotten some requests to turn off this behavior, in particular for the scenario when the ItemsSource changes, and this seems like another valid scenario where you may not want the built-in behavior.  We are considering adding some APIs to enable this in V2, and I've added your request to our internal database.  In addition, in V2 we should have better support for displaying hierarchal data in general.

If you'd like to try to enable this yourself, you have a few options.  One hacky you can do is to force the column widths to update whenever is appropriate in for app.  The way to do this is to set the column width to a small absolute value (like 10) and then back to Auto/SizeToHeader/SizeToCells programmatically.  This will trigger a new measure and arrange and auto-size the columns appropriately to whatever is visible.  I’m not sure what the UI will look like while this is happening since I haven’t tried it out, but if the end user can see it and it looks weird, you could do another hacky thing and take a screenshot of the grid before you change the column widths and put it over the actual grid to hide what’s going on in the background. 


If you’d prefer to do things the “right” way, then you’ll have to override Measure and find the largest DesiredWidth for all of the visible columns and set the column width to that value.  However, this is more difficult to do and may result in a performance hit. 


May 26, 2009 at 8:32 PM


The associated enumeration is Microsoft.Windows.Controls.DataGridLength;