Search code examples
wpfshadowstackpanel

WPF shadow on stackpanel controls


Is there a way to make the shadow of the first control in a StackPanel appear on top of the second control? I'm having trouble with this, look at the picture!
alt text http://img2.imageshack.us/img2/7073/issuef.png
Code sample:

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:sys="clr-namespace:System;assembly=mscorlib">
         <Grid>
         <StackPanel>
            <Border Height="100" Width="100" Background="Red">
                <Border.BitmapEffect>
                    <DropShadowBitmapEffect Color="Black" Direction="270" ShadowDepth="3" Opacity="1" Softness="2" />
                </Border.BitmapEffect>
            </Border>
                <Border Height="100" Width="100" Background="blue">
                </Border>
            </StackPanel>
        </Grid>
    </Page>

Solution

  • You can do use Panel.ZIndex="0" in each of the Borders to set the z order of the items directly from the XAML.

    <StackPanel>
        <Border Height="100" Width="100" Background="Red" Panel.ZIndex="1">
            <Border.BitmapEffect>
                <DropShadowBitmapEffect Color="Black" Direction="270" ShadowDepth="3" Opacity="1" Softness="2" />
            </Border.BitmapEffect>
        </Border>
        <Border Height="100" Width="100" Background="blue" Panel.ZIndex="0">
        </Border>
    </StackPanel>
    

    Or you can use StackPanel.SetZIndex(object, value) if you wanted to do it from code.