user5346812
user5346812

Reputation:

How to use GroupBox in C# WPF XAML to wrap only around specific elements

I'm trying to put some elements inside of a GroupBox in C# WPF XAML code. I'm not sure how to implement it correctly.

Basically I want:

<Grid HorizontalAlignment="Center" Width="1000">

 <GroupBox Header = "This stuff is in GroupBox ">
   <Label> Label in GroupBox </Label>
   <Label> Some other label in GroupBox  </Label>
 </GroupBox>

 <Label> This is not in the groupbox so don't put me in it! <Label>

</Grid>

Upvotes: 1

Views: 575

Answers (2)

Instead of the outermost Grid, use

<StackPanel Orientation="Vertical"> 
    <GroupBox Header = "This stuff is in GroupBox ">
        <Label> Label in GroupBox </Label>
        <Label> Some other label in GroupBox  </Label>
    </GroupBox>

    <Label> This is not in the groupbox so don't put me in it! <Label>
</StackPanel>

Either that or give the Grid some RowDefinitions, and give Grid.Row properties to the GroupBox and the outer Label. But the StackPanel is quick, and perfect for your case.

Upvotes: 1

Dean Chalk
Dean Chalk

Reputation: 20451

Your GroupBox and Label are in the same grid so will lay on top of each other.

Try this:

<Grid HorizontalAlignment="Center" Width="1000">
 <Grid.RowDefinitions>
   <RowDefinition Height="*" />
   <RowDefinition Height="Auto" />
 </Grid.RowDefinitions>
 <GroupBox Header = "This stuff is in GroupBox ">
   <Label> Label in GroupBox </Label>
   <Label> Some other label in GroupBox  </Label>
 </GroupBox>

 <Label Grid.Row="1"> This is not in the groupbox so don't put me in it! <Label>

</Grid>

Upvotes: 2

Related Questions