Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Windows Client Development, Woodworking, .NET Programming, CNC, Nature, and other topics.

Subscribe

Subscribe to my feed
Add to Technorati Favorites

My Book

Order my upcoming book, Silverlight in Action, covering Silverlight 4, ViewModel/MVVM, WCF RIA Services, MEF and more

About Pete Brown

Pete Brown is a Microsoft Developer Division Community Program Manager, focusing on Windows Client Development as well as a former Microsoft Silverlight MVP and INETA Speaker. Pete writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. read more

Community Events


who's online

AddThis Social Bookmark Button

Simple Xaml Drop Shadows in Silverlight 2

A while back, I showed how to vectorize "drop shadows" in Expression Design. It wasn't perfect (especially at the start and stop points of the line), but it was quick, easy, and didn't require any additional png files.

Since then, Expression Design 2 has been released in Beta form, and the Expression Design team dropped the ability to vectorize the brush strokes. From speaking with them, I understand and agree with their prioritization of other features, but it is still a bummer to see a potentially useful feature go away.

imageI need basic rectangular shadows on a project I'm working on. I didn't want to resort to using 8 png files to cover the whole rectangle so I decided to work up a quick way to handle shadows in xaml.

First, I put together a simple linear shadow, which you can see to the left. I have included the xaml for that below.

The approach is really simple: draw a bunch of adjacent lines with diminishing opacity. You can play with the colors and the opacity to get the effect you are looking for.

Xaml for a Simple Line Shadow

<Canvas>
    <Line Canvas.Left="0" Canvas.Top="0"  
          X1="250" X2="250" Y1="10" Y2="200" 
          Stroke="Black" Opacity="0.50" StrokeThickness="1"/>
    <Line Canvas.Left="0" Canvas.Top="0"  
          X1="251" X2="251" Y1="10" Y2="200" 
          Stroke="Black" Opacity="0.40" StrokeThickness="1"/>
    <Line Canvas.Left="0" Canvas.Top="0"  
          X1="252" X2="252" Y1="10" Y2="200" 
          Stroke="Black" Opacity="0.20" StrokeThickness="1"/>
    <Line Canvas.Left="0" Canvas.Top="0"  
          X1="253" X2="253" Y1="10" Y2="200" 
          Stroke="Black" Opacity="0.10" StrokeThickness="1"/>
</Canvas>

imageI was reasonably pleased with the result with the single line test, given the minimal effort it took, so I took a stab at rectangular shadows. You can see the result of that to the left.

I happened to use borders. You could easily use a Rectangle, border just happened to be what I was messing around with at the time. In many cases, a Rectangle or other shape will be more appropriate.

The result here looks fine too, as long as you don't scaletransform it. When you apply a transform, you will start to see banding due to the lines. In those cases, you might actually get better results just by biting the bullet and going with the png method, allowing Silverlight to scale and blur the pixels.

Note that you'll need to change the radius for each, as well as decreasing the x and y positions by 1 and increasing the width and height by 2.

Xaml for the Rectangle Shadow

<Canvas>
    <Border CornerRadius="10.0" Canvas.Left="4" Canvas.Top="4"  
            Width="100" Height="150" BorderThickness="1" 
            BorderBrush="Black" Opacity="0.50" />
    <Border CornerRadius="11" Canvas.Left="3" Canvas.Top="3"  
            Width="102" Height="152" BorderThickness="1" 
            BorderBrush="Black" Opacity="0.40" />
    <Border CornerRadius="12" Canvas.Left="2" Canvas.Top="2"  
            Width="104" Height="154" BorderThickness="1" 
            BorderBrush="Black" Opacity="0.20" />
    <Border CornerRadius="13" Canvas.Left="1" Canvas.Top="1"  
            Width="106" Height="156" BorderThickness="1" 
            BorderBrush="Black" Opacity="0.10" />
</Canvas>

If you have an arbitrary shape, defined by a path, it will be more challenging to create a workable drop shadow.

You can take the concepts here and get even better results by playing with the thickness and positions (fractional positions and additional lines should give a smoother line when transformed).

Ideally Silverlight will gain the effects capability of WPF and/or Flash in the future. Until then, we'll just have to fake it :)

  Add to Technorati Favorites
Posted: Thursday, April 03, 2008 6:11 PM by Pete.Brown

Comments

Simple Xaml Drop Shadows in Silverlight 2 said:

A while back, I # April 4, 2008 2:41 AM

Christopher Steen said:

ASP.NET Client Side Templating with jQuery [Via: Rick Strahl ] WPF Unwanted Multiple ClientBin Sub...
# April 6, 2008 11:58 PM

Sergei said:

Thanks for this, Pete! Very useful!
# April 16, 2008 4:10 PM

Jamie said:

Thanks for the guide, Pete! For anyone interested, I took what Pete did and modified it slightly to allow the borders to be nested within one another. This way you don't need to set the width or height on any of the border objects. The code can be found here
# October 20, 2008 7:09 PM

Douglas said:

Does the BitmapEffect exists in Silverlight ? I'm using it for dropdown shadow in my WPF Windows applications like this :
# December 8, 2008 7:31 AM

Pete.Brown said:

@Douglas

Nope, the BitmapEffect class doesn't exist in Silverlight.

FWIW, in WPF 3.5sp1, you'll want to use the new shaders instead of bitmap effects.

Pete

# December 8, 2008 4:32 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS