简体   繁体   中英

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>

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM