WPF Material Design Drawer打开后处于背景层,如何设置使其覆盖前景按钮?
Hey there, this is a classic WPF rendering layer issue—within the same Grid container, elements declared later are rendered on top (higher Z-order) by default. Since your Test Button is likely declared after the DrawerHost in the Grid, it's sitting above the opened drawer content. Here are two straightforward fixes:
Option 1: Reorder Elements in the Grid
Move the DrawerHost to be declared after the Test Button in your Grid. This way, the DrawerHost (and its drawer content) will naturally have a higher rendering priority:
<materialDesign:DialogHost> <Grid> <!-- Test Button comes first --> <Button> Test Button </Button> <!-- DrawerHost is declared later, so it renders on top --> <materialDesign:DrawerHost Margin="0" Height="350" Width="525" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"> <materialDesign:DrawerHost.LeftDrawerContent> <StackPanel Margin="16" Height="23" > <TextBlock Margin="4" HorizontalAlignment="Center">LEFT FIELD</TextBlock> <Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}" CommandParameter="{x:Static Dock.Left}" Margin="4" HorizontalAlignment="Center" Style="{DynamicResource MaterialDesignFlatButton}"> CLOSE THIS </Button> <Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}" Margin="4" HorizontalAlignment="Center" Style="{DynamicResource MaterialDesignFlatButton}"> CLOSE ALL </Button> </StackPanel> </materialDesign:DrawerHost.LeftDrawerContent> <Grid> <Grid VerticalAlignment="Center" HorizontalAlignment="Center"> <Button Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}" CommandParameter="{x:Static Dock.Left}" Grid.Row="1" Grid.Column="0" Margin="4"> <materialDesign:PackIcon Kind="ArrowLeft" /> </Button> </Grid> </Grid> </materialDesign:DrawerHost> </Grid> </materialDesign:DialogHost>
Option 2: Explicitly Set Panel.ZIndex
If you don't want to reorder your elements, you can assign a higher Panel.ZIndex value to the DrawerHost (default Z-index is 0; higher values mean higher rendering layers):
<materialDesign:DialogHost> <Grid> <materialDesign:DrawerHost Panel.ZIndex="1" Margin="0" Height="350" Width="525" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"> <!-- Your existing DrawerHost content here --> </materialDesign:DrawerHost> <Button> Test Button </Button> </Grid> </materialDesign:DialogHost>
Quick Explanation
The MaterialDesign DrawerHost's drawer content is designed to render on top of the host itself, but if another element in the same parent Grid has a higher natural or explicit Z-order, it will cover the drawer. Both fixes address this by ensuring the DrawerHost (and its drawer) sits above the Test Button in the rendering stack. The first option is generally preferred since it follows WPF's default layout behavior without extra attributes.
内容的提问来源于stack exchange,提问作者user19143260

