Search code examples
c#wpfgroupbox

WPF Groupbox height needs to change dynamically?


I am working with a groupbox in WPF, and Inside of the groupbox is a stackpanel. The stackpanel has items that are removed, and added to its children.

The problem I am experiencing is that Stackpanel within the groupbox is changing height, but the GroupBox is not, and so it is cutting off the rest of the added items within in the stackpanel.

How can I get the groupbox to adjust its height to the height of the stackpanel.

This must be done in code, not in XAML


Solution

  • Try setting the HorizontalAlignment and VerticalAlignment of the StackPanel to Stretch and ensure that the StackPanel doesn't have explicit Width and Height set.

    <StackPanel x:Name="stackPanel1" HorizontalAlignment="Stretch" VerticalAlignment="Strech">
    ...
    </StackPanel>
    

    or in code:

    stackPanel1.HorizontalAlignment = HorzontalAlignment.Stretch;
    stackPanel1.VerticalAlignment = VerticalAlignment.Stretch;
    

    Paste your code/xaml to help us make lesser assumptions.