savetruman
savetruman

Reputation: 145

Setting the Header of GroupBox in a GroupBox style

I'm getting XAML parse exceptions when using a certain GroupBox Style more than once. I'm keeping the style in UserControl.Resources.

Here is an example of a simplified style that does not cause a XAML parse exception:

<Style x:Key="MyGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource {x:Type GroupBox}}">
    <Setter Property="BorderThickness" Value="5" />
    <Setter Property="Header" Value="Hello World!" />
</Style>

Here is another one that does not cause a problem.

<Style x:Key="MyGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource {x:Type GroupBox}}">
    <Setter Property="BorderThickness" Value="5" />
    <Setter Property="Header">
        <Setter.Value>
            Hello World!
        </Setter.Value>
    </Setter>
</Style>

This one however does cause a problem:

<Style x:Key="MyGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource {x:Type GroupBox}}">
    <Setter Property="BorderThickness" Value="5" />
    <Setter Property="Header">
        <Setter.Value>
            <TextBlock Text="Hello World!" />
        </Setter.Value>
    </Setter>
</Style>

Again, it's only a problem when I use the style on more than one GroupBox. If I use it just once, I don't get the XAML parse exception. And it doesn't have to be just a TextBlock. I think it's any UIElement.

Any ideas? Thanks!

Upvotes: 0

Views: 852

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

You cannot set the content of the header to another UI control in the style. Try creating a datatemplate.

<Window.Resources>
    <Style x:Key="MyGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource {x:Type GroupBox}}">
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="Hello World!!!!!!"/>
                </DataTemplate>                    
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel>
    <GroupBox Style="{StaticResource MyGroupBoxStyle}" Height="200" Width="200"/>
    <GroupBox Style="{StaticResource MyGroupBoxStyle}" Height="200" Width="200"/>
</StackPanel>

Upvotes: 1

Related Questions