2
votes

This is what I have

   <Grid.RowDefinitions>
      <RowDefinition Height="Auto" MinHeight="25"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition />
  </Grid.RowDefinitions>

<Expander IsExpanded="False" Grid.Row="0" >
     <DataGrid name="FirstGrid" />
</Expander>
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch"  Height="5" />
<DataGrid Grid.Row="2" name="SecondGrid" />

When I click the expand button on the expander it correctly expands row 0 to the size of the DataGrid FirstGrid and it properly collapses the row as well. This does not work however if I expand the FirstGrid then manually resize that row by dragging the GridSplitter up or down and then pressing collapse button on the expander. What happens is that FirstGrid collapses but the row itself the FirstGrid is in does not. Any suggestions?

Thanks

2

2 Answers

3
votes

Rob is right, once you move the gridsplitter, the Height of the first Row will not be Auto anymore, so it will not respond to the change in the expander size In order for this to work you would need to add a behavior to the expander which will listen to the expander.expanded and collapsed event and update the grid row to be auto again. something like this:

public class GridColumnWidthReseterBehaviour : Behavior<Expander>
{
    private Grid _parentGrid;
    public int TargetGridRowIndex { get; set; }
    protected override void OnAttached()
    {
        AssociatedObject.Expanded += AssociatedObject_Expanded;
        AssociatedObject.Collapsed += AssociatedObject_Collapsed;
        FindParentGrid();
    }

    private void FindParentGrid()
    {
        DependencyObject parent = LogicalTreeHelper.GetParent(AssociatedObject);
        while (parent != null)
        {
            if (parent is Grid)
            {
                _parentGrid = parent as Grid;
                return;
            }
            parent = LogicalTreeHelper.GetParent(AssociatedObject);
        }
    }

    void AssociatedObject_Collapsed(object sender, System.Windows.RoutedEventArgs e)
    {
        _parentGrid.RowDefinitions[TargetGridRowIndex].Height= GridLength.Auto;
    }

    void AssociatedObject_Expanded(object sender, System.Windows.RoutedEventArgs e)
    {
        _parentGrid.RowDefinitions[TargetGridRowIndex].Height= GridLength.Auto;
    }
}

And you use it like this:

 <Expander ...>
    <interactivity:Interaction.Behaviors>
                <behaviors:GridColumnWidthReseterBehaviour TargetGridRowIndex="0"></behaviors:GridColumnWidthReseterBehaviour>
            </interactivity:Interaction.Behaviors>

...

0
votes

As soon as you move the gridsplitter, the Height of the first Row will not be Auto anymore, but a certain value, like 70. After that it doesn't matter if some child in that row changes its height.

Combining a splitter with some auto-sized child/row is very difficult; you can take a look at the side expander in Microsoft Outlook; i suspect that this is what you want. If so, you shouldn't use an expander, because a regular expander contains a StackPanel so its children are always auto-sizing in the expand direction. I'm not sure what you want, but i think going for a custom control is your best option.