Steve
Steve

Reputation: 11963

Expander.Content only applied to one

I am trying to apply style to multiple expanders which will have similar look and functionality. But the problem is that if I style Expander.Content only one of many will have content and the rest are all empty.

enter image description here

Minimal example:

<Style TargetType="Expander" BasedOn="{StaticResource {x:Type Expander}}">
    <Setter Property="Header">
        <Setter.Value>
            Header
        </Setter.Value>
    </Setter>
    <Setter Property="Content">
        <Setter.Value>
            <TextBlock Text="1"/>
        </Setter.Value>
    </Setter>
</Style>

<!-- .... -->

<Expander Grid.Row="0"/>
<Expander Grid.Row="1"/>
<Expander Grid.Row="2"/>

as you see only the second row has content (1) during design time. During run time only the last expander has content (1).

If I click on the first expander the content (1) from the third expander actually moved to the first expander.

Why is this happening? And how to fix it?

Upvotes: 0

Views: 37

Answers (1)

brunnerh
brunnerh

Reputation: 184441

Styles only create one instance, which for visual elements can only be the child of a single parent. Set the ContentTemplate instead of the Content directly. (Templates just describe what is to be created by the control using it, so it can be shared.)

Upvotes: 1

Related Questions