sohum
sohum

Reputation: 3247

HeaderTemplate with multiple items

I'm trying to write a HeaderTemplate for an extender. So far, I've noticed all the examples use the {Binding} keyword to get the data from the header. However, what happens if there are multiple controls within the Header? How do I specify that those controls should be inserted at a specific location?

<Window.Resources>
    <Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <!-- what do I put in here? -->
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Expander Style="{StaticResource ExpanderStyle}">
    <Expander.Header>
        <StackPanel Orientation="Horizontal">
            <TextBlock>Some Text</TextBlock>
            <TextBlock Text="{Binding SomeBinding}" />
            <Button />
        </StackPanel>
    </Expander.Header>
    <Image Source="https://www.google.com/logos/2012/steno12-hp.jpg" />
</Expander>

Should I be moving my binding into the HeaderTemplate in the style and just overwriting whatever the Header in the Expander is?

Upvotes: 4

Views: 5491

Answers (2)

Rachel
Rachel

Reputation: 132618

You can use ContentPresenter to insert whatever the usual content would be into your Template

For example:

<Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border BorderBrush="Blue" BorderThickness="2">
                    <ContentPresenter />
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 3

Nikita Martyanov
Nikita Martyanov

Reputation: 321

Header's property Content could contain only one object.

If you merge these object in one panel:

<StackPanel>
    <TextBlock>Some Text</TextBlock>
    <TextBlock Text="{Binding SomeBinding}" />
    <Button />
</StackPanel>

then in template you could use {binding}

Upvotes: 0

Related Questions