Scott Ferguson
Scott Ferguson

Reputation: 7830

How do I change the WPF Expander Header Text when expanded or collapsed?

Using a WPF expander, I want the Header to change from See More to See Less when the control is expanded, and back to See More when it's collapsed again. I'd prefer a WPF pure solution, rather than a C# or other code behind method. I get the feeling this should be easy, but I'm struggling for the right terms to get a solution via google.

Thanks!

Upvotes: 11

Views: 13012

Answers (2)

cypizek
cypizek

Reputation: 337

Old (I know), but this can be done with using two simple Trigger without binding:

<Expander>
    <Expander.Style>
        <Style TargetType="Expander" >
            <Style.Triggers>
                <Trigger Property="IsExpanded" Value="True">
                    <Setter Property="Header" Value="See Less" />
                </Trigger>
                <Trigger Property="IsExpanded" Value="False">
                    <Setter Property="Header" Value="See More" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Expander.Style>
</Expander>

Upvotes: 8

Matt Hamilton
Matt Hamilton

Reputation: 204209

You could probably do this in a style trigger:

<Expander>
    <Expander.Style>
        <Style TargetType="Expander">
            <Setter Property="IsExpanded" Value="False" />
            <Setter Property="Header" Value="See More" />

            <Style.Triggers>
                <DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True">
                    <Setter Property="Header" Value="See Less" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Expander.Style>
</Expander>

That's untested, but it should give you something to go on.

Upvotes: 23

Related Questions