Search code examples
wpfxamldropshadoweffect

DropShadowEffect WPF


I am new to WPF/XAML coding. I am trying to add a drop shadow effect to a Rectangle shape. The XAML for the rectangle is:

CODE 1

<Rectangle HorizontalAlignment="Left" Height="552" Margin="2,10,0,0" 
             VerticalAlignment="Top" Width="427" StrokeThickness="4" 
             Fill="#FF484A4D" Grid.Column="1"/>

The code for DropShadowEffect is:

CODE 2

<Rectangle.Effect>
    <DropShadowEffect x:name="Dshadow" BlurRadius="10" ShadowDepth="0" Color="Black"/>
</Rectangle.Effect>

The problem is, I can't combine/use these two codes together. When I arrange CODE 2 after CODE 1, it doesn't work. How should I fix/arrange these codes?


Solution

  • You should not close your Rectangle at the first line.

    <Rectangle HorizontalAlignment="Left" 
               Height="552" 
               Margin="2,10,0,0" 
               VerticalAlignment="Top" 
               Width="427" 
               StrokeThickness="4" 
               Fill="#FF484A4D" 
               Grid.Column="1">
         <Rectangle.Effect>
             <DropShadowEffect BlurRadius="10" 
                               ShadowDepth="0" 
                               Color="Black"/>
         </Rectangle.Effect>    
     </Rectangle>
    

    You need to access a Property of Rectangle - you cannot do that if you have already closed that Element.