Rabcor Rabcor - 2 months ago 11
C# Question

Placing more than one element into a GroupBox in WPF

I'm trying to make a GroupBox with a few (radio)buttons in it. But in the samples i'm using checkbox.

<GroupBox Header="Aðgerðir" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="270,0,0,178" Height="106" Width="176">
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
</GroupBox>


The above does not work and visual studio says "Invalid Markup".

This here works fine though

<GroupBox Header="Aðgerðir" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="270,0,0,178" Height="106" Width="176">
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</GroupBox>


I don't get it. It says "Content can only be set once" if i run the debugger, but erasing the Content part of the checkbox seems to have no effect.

Answer

The content of the GroupBox can only be set once, meaning that it can only have a single control inside it. If you want to two radio button, place them in a stack panel or a grid.

GroupBox Overview

<GroupBox Header="Aðgerðir" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="270,0,0,178" Height="106" Width="176">
    <StackPanel>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
    </StackPanel>    
</GroupBox>
Comments