Expanders in Grid

后端 未结 2 1460
野的像风
野的像风 2021-02-01 07:43

This is going to be straight forward no doubt, but for what ever reason, my mind is drawing a blank on it.

I\'ve got a small, non-resizeable window (325x450) which has 3

2条回答
  •  旧时难觅i
    2021-02-01 07:59

    If you don't mind a little code-behind, you could probably hook into the Expanded/Collapsed events, find the parent Grid, get the RowDefinition for the expander, and set the value equal to * if its expanded, or Auto if not.

    For example,

    Expander ex = sender as Expander;
    Grid parent = FindAncestor(ex);
    int rowIndex = Grid.GetRow(ex);
    
    if (parent.RowDefinitions.Count > rowIndex && rowIndex >= 0)
        parent.RowDefinitions[rowIndex].Height = 
            (ex.IsExpanded ? new GridLength(1, GridUnitType.Star) : GridLength.Auto);
    

    And the FindAncestor method is defined as this:

    public static T FindAncestor(DependencyObject current)
    where T : DependencyObject
    {
        // Need this call to avoid returning current object if it is the 
        // same type as parent we are looking for
        current = VisualTreeHelper.GetParent(current);
    
        while (current != null)
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        };
        return null;
    }
    

提交回复
热议问题