Search code examples
wpfxamlclickableexpander

WPF expander not expanded above buttons make buttons unclickable


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?


Solution

  • 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);
        }