Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing a WPF Expander from expanding when header is clicked

Tags:

wpf

xaml

expander

How can I prevent a WPF Expander from expanding when its header is clicked? I would like my Expander to expand or collapse only when the expand button itself is clicked.

I imagine the answer has something to do with canceling a bubbled event. If possible I would like to implement the solution in XAML while avoiding retemplating the entire Expander.

like image 208
sourcenouveau Avatar asked Sep 08 '09 20:09

sourcenouveau


2 Answers

There is actually a much simpler XAML solution than modifying templates. Simply DON'T use the Expander's header property in this case. Instead, cover the expander with your own styled TextBlock.

<Application.Resources>
    <Style x:Key="ExpanderHeader" TargetType="{x:Type TextBlock}">
        <Setter Property="Height" Value="22" />
        <Setter Property="Margin" Value="21,0,0,0" />
        <Setter Property="Padding" Value="9,3,0,0" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
</Application.Resources>

<Grid>
    <Expander>
        <TextBlock Text="I am some content. I have disowned my default header." Margin="10,5" />
    </Expander>
    <TextBlock Text="I'm filling in for the default header. You'll like me better anyway."
               Style="{StaticResource ResourceKey=ExpanderHeader}"/>
</Grid>
like image 106
bugged87 Avatar answered Sep 30 '22 01:09

bugged87


The header is the button that creates the event, so you need to change the template for your Expander to just have the expander icon as the button.

Here is a post of how to change the expander.

like image 22
Simeon Pilgrim Avatar answered Sep 30 '22 00:09

Simeon Pilgrim