WPF expander not expanded above buttons make buttons unclickable

620 views Asked by At

In a WPF form, I have an expander that expands above other controls, such as buttons :

<Expander Grid.Row="0" Panel.ZIndex="99" Name="searchMenuExpander" Header="Search menu" FontWeight="Bold" HorizontalAlignment="Stretch" Margin="10,10,0,0" MinWidth="510" MinHeight="200" VerticalAlignment="Top" Foreground="MidnightBlue" Cursor="Arrow">
    <Grid Background="White">
        <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <TextBlock Text="Name" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,10,0,0" Width="65" Height="20" Cursor="Arrow" />
        <TextBox Name="nameSearchTextBox"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="115,10,0,0" Width="100" Height="20"/>
    </Grid>
</Expander>
<Button Content="Button" Height="34" Width="85"/>

My problem is that my controls that are behind the expander are not clickable anymore, even when the expander is not expanded. What is happening? How could I get rid of this bug?

2

There are 2 answers

0
Sithered On BEST ANSWER

I found a way of doing it, with code behind. I put my Expander in a Canvas and I added to my Expander two events:

Expanded="searchMenuExpander_Expanded" and Collapsed="searchMenuExpander_Collapsed" which are defined as:

    private void searchMenuExpander_Expanded(object sender, RoutedEventArgs e)
    {
        Canvas.SetZIndex(searchMenuCanvas, 99);
    }

    private void searchMenuExpander_Collapsed(object sender, RoutedEventArgs e)
    {
        Canvas.SetZIndex(searchMenuCanvas, 0);
    }
2
Kryptos On

Assuming you have a Grid that is the parent of both your Expander and your Button, you need to put the Button on a different row.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Expander Grid.Row="0">
    </Expander>
    <Button Grid.Row="1" />
</Grid>

By setting the Height of the first row (which contains the expander) to Auto, you ensure that it will be minimized when the expander is not-expanded (only the header part of the expander will occupy space).