title: How to draw on top of WPF effects in data grid control?
body: <p>I have a data grid control where I am using a custom effect called FastShadow which is like a glow.</p>
  <p>I want the effect to glow outside its bounds, so that's fine but when I draw another shape on top, I don't want this shape to be affected. In this case, it's the Green/Blue Rectangle shapes.</p>
  <p>As you can see these rectangle shapes get discolored by the glow effect. The ones without the glow effect behind them looks fine.</p>
  <p><a href="https://i.stack.imgur.com/OBHJ8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OBHJ8.png" alt="enter image description here" /></a></p>
  <p>Here is the XAML:</p>
  <pre><code>&lt;Window x:Class=&quot;test.MainWindow&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
      xmlns:local=&quot;clr-namespace:test&quot;
      mc:Ignorable=&quot;d&quot;
      Name=&quot;myMainWindow&quot;
      SizeToContent=&quot;Width&quot;
      DataContext=&quot;{Binding RelativeSource={RelativeSource Self}}&quot;
      Title=&quot;Profit Tracker&quot;
      WindowStyle=&quot;None&quot;
      Topmost=&quot;True&quot;
      Height=&quot;426&quot;&gt;
  
      &lt;Window.Resources&gt;
  
          &lt;Style x:Key=&quot;DataGridColumnSeparatorStyle&quot; TargetType=&quot;DataGridCell&quot;&gt;
              &lt;Setter Property=&quot;Template&quot;&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate&gt;
                          &lt;Rectangle VerticalAlignment=&quot;Stretch&quot; HorizontalAlignment=&quot;Stretch&quot; Fill=&quot;#1e90ff&quot;/&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  
          &lt;Style x:Key=&quot;DataGridColumnAlarmStyle&quot; TargetType=&quot;DataGridCell&quot;&gt;
              &lt;Setter Property=&quot;Template&quot;&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate&gt;
                          &lt;Rectangle VerticalAlignment=&quot;Stretch&quot; HorizontalAlignment=&quot;Stretch&quot; Fill=&quot;#000000&quot;/&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  
  
  
          &lt;Style TargetType=&quot;{x:Type DataGrid}&quot;&gt;
              &lt;Setter Property=&quot;Background&quot; Value=&quot;#FFF&quot; /&gt;
              &lt;Setter Property=&quot;AlternationCount&quot; Value=&quot;2&quot; /&gt;
              &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Red&quot; /&gt;
              &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;0&quot; /&gt;
          &lt;/Style&gt;
  
          &lt;Style x:Key=&quot;RowStyleWithAlternation&quot; TargetType=&quot;DataGridRow&quot;&gt;
              &lt;Setter Property=&quot;Background&quot; Value=&quot;#141414&quot;/&gt;
              &lt;Setter Property=&quot;Foreground&quot; Value=&quot;White&quot;/&gt;
              &lt;Setter Property=&quot;FontWeight&quot; Value=&quot;Normal&quot;/&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property=&quot;AlternationIndex&quot; Value=&quot;0&quot;&gt;
                      &lt;Setter Property=&quot;Background&quot; Value=&quot;#141414&quot;/&gt;
                  &lt;/Trigger&gt;
                  &lt;Trigger Property=&quot;AlternationIndex&quot; Value=&quot;1&quot;&gt;
                      &lt;Setter Property=&quot;Background&quot; Value=&quot;#282828&quot;/&gt;
                  &lt;/Trigger&gt;
                  &lt;Trigger Property=&quot;IsSelected&quot; Value=&quot;True&quot;&gt;
                      &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Red&quot; /&gt;
                      &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;1&quot; /&gt;
                      &lt;Setter Property=&quot;Margin&quot; Value=&quot;-1,0,0,0&quot; /&gt;
                  &lt;/Trigger&gt;
                  &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
                      &lt;!--&lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;#2eff00&quot; /&gt;
                      &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;1&quot; /&gt;--&gt;
                      &lt;Setter Property=&quot;Background&quot; Value=&quot;Orange&quot;/&gt;
                      &lt;!--&lt;Setter Property=&quot;Margin&quot; Value=&quot;-1,0,0,0&quot; /&gt;--&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
  
          &lt;Style TargetType=&quot;DataGridCell&quot;&gt;
              &lt;Setter Property=&quot;TextBlock.TextAlignment&quot; Value=&quot;Center&quot; /&gt;
              &lt;Setter Property=&quot;Template&quot;&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType=&quot;{x:Type DataGridCell}&quot;&gt;
                          &lt;Grid Background=&quot;{TemplateBinding Background}&quot;&gt;
                              &lt;ContentPresenter VerticalAlignment=&quot;Stretch&quot;/&gt;
                          &lt;/Grid&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
              &lt;Setter Property=&quot;Padding&quot; Value=&quot;0&quot;/&gt;
              &lt;Setter Property=&quot;Margin&quot; Value=&quot;0&quot;/&gt;
              &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;0&quot;/&gt;
              &lt;Setter Property=&quot;FocusVisualStyle&quot; Value=&quot;{x:Null}&quot; /&gt;
  
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property=&quot;IsSelected&quot; Value=&quot;True&quot;&gt;
                      &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;#1e90ff&quot; /&gt;
                      &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;1&quot; /&gt;
                      &lt;!--&lt;Setter Property=&quot;Background&quot; Value=&quot;Red&quot;/&gt;--&gt;
                  &lt;/Trigger&gt;
  
  
  
                  &lt;MultiDataTrigger&gt;
                      &lt;MultiDataTrigger.Conditions&gt;
                          &lt;Condition Binding=&quot;{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}&quot; Value=&quot;4&quot;/&gt;
                      &lt;/MultiDataTrigger.Conditions&gt;
                      &lt;MultiDataTrigger.Setters&gt;
                          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
                      &lt;/MultiDataTrigger.Setters&gt;
                  &lt;/MultiDataTrigger&gt;
  
                  &lt;Trigger Property=&quot;IsSelected&quot; Value=&quot;True&quot;&gt;
                      &lt;Setter Property=&quot;Background&quot; Value=&quot;{x:Null}&quot;/&gt;
                      &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;{x:Null}&quot;/&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
  
          &lt;Style TargetType=&quot;DataGridColumnHeader&quot;&gt;
              &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Center&quot; /&gt;
          &lt;/Style&gt;
  
          &lt;Style TargetType=&quot;{x:Type ProgressBar}&quot;&gt;
              &lt;Setter Property=&quot;Padding&quot; Value=&quot;0&quot;/&gt;
              &lt;Setter Property=&quot;Margin&quot; Value=&quot;0&quot;/&gt;
              &lt;Setter Property=&quot;Template&quot;&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType=&quot;ProgressBar&quot;&gt;
                          &lt;Border BorderThickness=&quot;1&quot; Background=&quot;#006400&quot; CornerRadius=&quot;0&quot; Padding=&quot;0&quot;&gt;
                              &lt;Grid x:Name=&quot;PART_Track&quot;&gt;
                                  &lt;Rectangle x:Name=&quot;PART_Indicator&quot; HorizontalAlignment=&quot;Left&quot; Fill=&quot;#75001D&quot; /&gt;
                              &lt;/Grid&gt;
                          &lt;/Border&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  
          &lt;CollectionViewSource Source=&quot;{Binding Coins}&quot; IsLiveSortingRequested=&quot;True&quot; x:Key=&quot;MyKey&quot; /&gt;
  
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
  
          &lt;Grid.LayoutTransform&gt;
              &lt;ScaleTransform ScaleX=&quot;{Binding ViewScale, ElementName=myMainWindow}&quot; ScaleY=&quot;{Binding ViewScale, ElementName=myMainWindow}&quot; /&gt;
          &lt;/Grid.LayoutTransform&gt;
  
          &lt;DataGrid Name=&quot;dataGrid&quot; ItemsSource=&quot;{Binding Source={StaticResource MyKey}}&quot; SelectionMode=&quot;Single&quot; GridLinesVisibility=&quot;None&quot; HorizontalScrollBarVisibility=&quot;Hidden&quot; RowHeaderWidth=&quot;0&quot; IsReadOnly=&quot;True&quot; CanUserAddRows=&quot;False&quot; CanUserResizeColumns=&quot;False&quot; CanUserResizeRows=&quot;False&quot; AutoGenerateColumns=&quot;False&quot; RowStyle=&quot;{StaticResource RowStyleWithAlternation}&quot;&gt;
              &lt;DataGrid.Resources&gt;
                  &lt;SolidColorBrush x:Key=&quot;{x:Static SystemColors.HighlightBrushKey}&quot; Color=&quot;Transparent&quot; /&gt;
                  &lt;SolidColorBrush x:Key=&quot;{x:Static SystemColors.ControlBrushKey}&quot; Color=&quot;Transparent&quot; /&gt;
              &lt;/DataGrid.Resources&gt;
  
              &lt;DataGrid.Columns&gt;
  
                  &lt;DataGridTemplateColumn Header=&quot;&quot; MaxWidth=&quot;40&quot;&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
  
                              &lt;Grid&gt;
                              &lt;Grid Visibility=&quot;{Binding IsVisible}&quot;&gt;
  
                                  &lt;Border Margin=&quot;1&quot; VerticalAlignment=&quot;Stretch&quot; BorderThickness=&quot;0&quot; Background=&quot;#ffff00&quot; HorizontalAlignment=&quot;Left&quot; Width=&quot;7&quot;&gt;
                                      &lt;local:FastShadow Color=&quot;Yellow&quot; ShadowDepth=&quot;0&quot; Direction=&quot;0&quot; BlurRadius=&quot;80&quot; Opacity=&quot;0.2&quot;&gt;
                                      &lt;/local:FastShadow&gt;
                                  &lt;/Border&gt;
  
                                  &lt;Border Margin=&quot;1&quot; VerticalAlignment=&quot;Stretch&quot; BorderThickness=&quot;0&quot; Background=&quot;#ffff00&quot; HorizontalAlignment=&quot;Left&quot; Width=&quot;7&quot;&gt;
                                      &lt;local:FastShadow Color=&quot;#ff8d0a&quot; ShadowDepth=&quot;0&quot; Direction=&quot;0&quot; BlurRadius=&quot;80&quot; Opacity=&quot;0.4&quot;&gt;
                                      &lt;/local:FastShadow&gt;
                                  &lt;/Border&gt;
  
                                  &lt;Border BorderBrush=&quot;#ff8d0a&quot; BorderThickness=&quot;1&quot; CornerRadius=&quot;3&quot; Background=&quot;#ffff00&quot;&gt;
                                      &lt;Border Margin=&quot;1&quot; BorderBrush=&quot;#fff533&quot; BorderThickness=&quot;4&quot; CornerRadius=&quot;3&quot; ClipToBounds=&quot;False&quot; HorizontalAlignment=&quot;Stretch&quot;&gt;
                                          &lt;Border.Effect&gt;
                                              &lt;BlurEffect Radius=&quot;50&quot;/&gt;
                                          &lt;/Border.Effect&gt;
                                      &lt;/Border&gt;
                                  &lt;/Border&gt;
                              &lt;/Grid&gt;
  
                                  &lt;Grid Height=&quot;5&quot; VerticalAlignment=&quot;Bottom&quot;&gt;
                                      &lt;Grid Background=&quot;GreenYellow&quot;&gt;
                                          &lt;Rectangle Fill=&quot;DarkGreen&quot; HorizontalAlignment=&quot;Right&quot; Width=&quot;50&quot;&gt;
                                          &lt;/Rectangle&gt;
                                      &lt;/Grid&gt;
                                      &lt;Grid &gt;
                                          &lt;Rectangle Fill=&quot;DarkBlue&quot; HorizontalAlignment=&quot;Right&quot; Width=&quot;20&quot;&gt;
                                          &lt;/Rectangle&gt;
                                      &lt;/Grid&gt;
                                  &lt;/Grid&gt;
  
                              &lt;/Grid&gt;
                              
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
  
                  &lt;DataGridTemplateColumn MinWidth=&quot;0&quot; Width=&quot;2&quot; CellStyle=&quot;{StaticResource DataGridColumnSeparatorStyle}&quot;/&gt;
  
                  &lt;DataGridTemplateColumn Header=&quot;&quot; Width=&quot;24&quot;&gt;
                      &lt;DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;Ellipse Width=&quot;8&quot; Height=&quot;8&quot; Fill=&quot;Blue&quot; /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;/DataGridTemplateColumn&gt;
  
  
  
                  &lt;DataGridTextColumn Header=&quot;&quot; Width=&quot;60&quot;/&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  <p>Code:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace test
  {
      public partial class MainWindow : Window, INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
          protected void SetField&lt;T&gt; ( ref T field, T value, string propertyName )
          {
              if ( !EqualityComparer&lt;T&gt;.Default.Equals ( field, value ) )
              {
                  field = value;
                  PropertyChanged?.Invoke ( this, new PropertyChangedEventArgs ( propertyName ) );
              }
          }
  
  
          decimal viewScale = 1;
          public decimal ViewScale
          {
              get =&gt; this.viewScale;
              set =&gt; SetField ( ref this.viewScale, value,
                  &quot;ViewScale&quot;
                  );
          }
  
          ObservableCollection&lt;Coin&gt; _coins;
          public ObservableCollection&lt;Coin&gt; Coins { get =&gt; _coins; set =&gt; SetField ( ref _coins, value, nameof ( _coins ) ); }
          public ICollectionView CollectionView;
  
          public MainWindow ( )
          {
              this.Coins = new ObservableCollection&lt;Coin&gt; ( );
              for ( int i = 0; i &lt; 25; ++i )
                  this.Coins.Add ( new Coin ( &quot;Coin 1&quot;, i ) );
  
              this.Coins [ 1 ].IsVisible = Visibility.Visible;
              this.Coins [ 4 ].IsVisible = Visibility.Visible;
              this.Coins [ 7 ].IsVisible = Visibility.Visible;
  
              this.DataContext = this;
  
              InitializeComponent ( );
  
              this.PreviewKeyDown += MainWindow_PreviewKeyDown;
              this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
          }
  
          private void MainWindow_MouseLeftButtonDown ( object sender, MouseButtonEventArgs e )
          {
              if ( ( Keyboard.Modifiers &amp; ModifierKeys.Alt ) == ModifierKeys.Alt )
                  DragMove ( );
          }
  
          void MainWindow_PreviewKeyDown ( object sender, KeyEventArgs e )
          {
              if ( e.Key == Key.Home )
              {
                  this.dataGrid.ScrollIntoView ( this.dataGrid.Items [ 0 ] );
              }
              if ( e.Key == Key.End )
              {
                  this.dataGrid.ScrollIntoView ( this.dataGrid.Items [ this.dataGrid.Items.Count - 1 ] );
                  this.dataGrid.UpdateLayout ( );
                  this.dataGrid.ScrollIntoView ( this.dataGrid.Items [ 0 ] );
              }
              else if ( e.Key == Key.F12 )
              {
                  this.ViewScale += 0.1m;
              }
          }
      }
  
      public class Coin
      {
          public string Symbol { get; set; }
          public int PNL { get; set; }
          public SolidColorBrush Color2 { get; set; }
  
          public Visibility IsVisible { get; set; }
  
          public Coin ( string symbol, int pnl )
          {
              this.Symbol = symbol;
              this.PNL = pnl;
  
              Random rnd = new Random ( );
              Color c = Color.FromRgb ( ( byte ) rnd.Next ( 256 ), ( byte ) rnd.Next ( 256 ), ( byte ) rnd.Next ( 256 ) );
  
              this.Color2 = new SolidColorBrush ( c );
  
              this.IsVisible = Visibility.Hidden;
          }
      }
  }
  </code></pre>
  <p><strong>class FastShadow</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace test
  {
      using System.Windows.Shapes;
      using System.Windows.Controls;
      using System.Windows;
      using System.Windows.Media;
      using System;
      using System.ComponentModel;
  
      /// &lt;summary&gt;
      /// Emulates the System.Windows.Media.Effects.DropShadowEffect using
      /// rectangles and gradients, which performs a million times better
      /// and won't randomly crash a good percentage of your end-user's 
      /// video drivers.
      /// &lt;/summary&gt;
      public class FastShadow : Decorator
      {
  
          #region Dynamic Properties
  
          public static readonly DependencyProperty ColorProperty =
                  DependencyProperty.Register (
                          &quot;Color&quot;,
                          typeof ( Color ),
                          typeof ( FastShadow ),
                          new FrameworkPropertyMetadata (
                                  Color.FromArgb ( 0x71, 0x00, 0x00, 0x00 ),
                                  FrameworkPropertyMetadataOptions.AffectsRender ) );
  
          /// &lt;summary&gt;
          /// The Color property defines the Color used to fill the shadow region. 
          /// &lt;/summary&gt; 
          [Category ( &quot;Common Properties&quot; )]
          public Color Color
          {
              get { return ( Color ) GetValue ( ColorProperty ); }
              set { SetValue ( ColorProperty, value ); }
          }
  
          /// &lt;summary&gt;
          /// Distance from centre, why MS don't call this &quot;distance&quot; beats
          /// me.. Kept same as other Effects for consistency.
          /// &lt;/summary&gt;
          [Category ( &quot;Common Properties&quot; ), Description ( &quot;Distance from centre&quot; )]
          public double ShadowDepth
          {
              get { return ( double ) GetValue ( ShadowDepthProperty ); }
              set { SetValue ( ShadowDepthProperty, value ); }
          }
  
          // Using a DependencyProperty as the backing store for ShadowDepth.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty ShadowDepthProperty =
              DependencyProperty.Register ( &quot;ShadowDepth&quot;, typeof ( double ), typeof ( FastShadow ),
              new FrameworkPropertyMetadata (
                  5.0, FrameworkPropertyMetadataOptions.AffectsRender,
                      new PropertyChangedCallback ( ( o, e ) =&gt;
                      {
                          FastShadow f = o as FastShadow;
                          if ( ( double ) e.NewValue &lt; 0 )
                              f.ShadowDepth = 0;
                      } ) ) );
  
  
          /// &lt;summary&gt;
          /// Size of the shadow
          /// &lt;/summary&gt;
          [Category ( &quot;Common Properties&quot; ), Description ( &quot;Size of the drop shadow&quot; )]
          public double BlurRadius
          {
              get { return ( double ) GetValue ( BlurRadiusProperty ); }
              set { SetValue ( BlurRadiusProperty, value ); }
          }
  
          // Using a DependencyProperty as the backing store for BlurRadius.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty BlurRadiusProperty =
              DependencyProperty.Register ( &quot;BlurRadius&quot;, typeof ( double ), typeof ( FastShadow ),
              new FrameworkPropertyMetadata ( 10.0,
                  FrameworkPropertyMetadataOptions.AffectsRender,
                      new PropertyChangedCallback ( ( o, e ) =&gt;
                      {
                          FastShadow f = o as FastShadow;
                          if ( ( double ) e.NewValue &lt; 0 )
                              f.BlurRadius = 0;
                      } ) ) );
  
  
          /// &lt;summary&gt;
          /// Angle of the shadow
          /// &lt;/summary&gt;
          [Category ( &quot;Common Properties&quot; ), Description ( &quot;Angle of the shadow&quot; )]
          public int Direction
          {
              get { return ( int ) GetValue ( DirectionProperty ); }
              set { SetValue ( DirectionProperty, value ); }
          }
  
          // Using a DependencyProperty as the backing store for Direction.  This enables animation, styling, binding, etc...
          public static readonly DependencyProperty DirectionProperty =
              DependencyProperty.Register ( &quot;Direction&quot;, typeof ( int ), typeof ( FastShadow ),
              new FrameworkPropertyMetadata ( 315, FrameworkPropertyMetadataOptions.AffectsRender ) );
  
  
          #endregion Dynamic Properties
  
          #region Protected Methods
  
          protected override void OnRender ( DrawingContext drawingContext )
          {
  
              double distance = Math.Max ( 0, ShadowDepth );
              double blurRadius = Math.Max ( BlurRadius, 0 );
              double angle = Direction + 45; // Make it behave the same as DropShadowEffect
  
              Rect shadowBounds = new Rect ( new Point ( 0, 0 ),
                               new Size ( RenderSize.Width, RenderSize.Height ) );
  
              shadowBounds.Inflate ( blurRadius, blurRadius );
  
              Color color = Color;
  
              // Transform angle for &quot;Direction&quot;
              double angleRad = angle * Math.PI / 180.0;
              double xDispl = distance;
              double yDispl = distance;
              double newX = xDispl * Math.Cos ( angleRad ) - yDispl * Math.Sin ( angleRad );
              double newY = yDispl * Math.Cos ( angleRad ) + xDispl * Math.Sin ( angleRad );
  
              TranslateTransform translate = new TranslateTransform ( newX, newY );
              Rect transformed = translate.TransformBounds ( shadowBounds );
  
              // Hint: you can make the blur radius consume more &quot;centre&quot;
              //       region of the bounding box by doubling this here
              // blurRadius = blurRadius * 2;
  
              // Build a set of rectangles for the shadow box
              Rect [ ] edges = new Rect [ ] {
                  new Rect(new Point(transformed.X,transformed.Y), new Size(blurRadius,blurRadius)), // TL
                  new Rect(new Point(transformed.X+blurRadius,transformed.Y), new Size(Math.Max(transformed.Width-(blurRadius*2),0),blurRadius)), // T
                  new Rect(new Point(transformed.Right-blurRadius,transformed.Y), new Size(blurRadius,blurRadius)), // TR
                  new Rect(new Point(transformed.Right-blurRadius,transformed.Y+blurRadius), new Size(blurRadius,Math.Max(transformed.Height-(blurRadius*2),0))), // R
                  new Rect(new Point(transformed.Right-blurRadius,transformed.Bottom-blurRadius), new Size(blurRadius,blurRadius)), // BR
                  new Rect(new Point(transformed.X+blurRadius,transformed.Bottom-blurRadius), new Size(Math.Max(transformed.Width-(blurRadius*2),0),blurRadius)), // B
                  new Rect(new Point(transformed.X,transformed.Bottom-blurRadius), new Size(blurRadius,blurRadius)), // BL
                  new Rect(new Point(transformed.X,transformed.Y+blurRadius), new Size(blurRadius,Math.Max(transformed.Height-(blurRadius*2),0))), // L
                  new Rect(new Point(transformed.X+blurRadius,transformed.Y+blurRadius), new Size(Math.Max(transformed.Width-(blurRadius*2),0),Math.Max(transformed.Height-(blurRadius*2),0))), // C
              };
  
              // Gradient stops look a lot prettier than
              // a perfectly linear gradient..
              GradientStopCollection gsc = new GradientStopCollection ( );
              Color stopColor = color;
              stopColor.A = ( byte ) ( color.A );
              gsc.Add ( new GradientStop ( color, 0.0 ) );
              stopColor.A = ( byte ) ( .74336 * color.A );
              gsc.Add ( new GradientStop ( stopColor, 0.1 ) );
              stopColor.A = ( byte ) ( .38053 * color.A );
              gsc.Add ( new GradientStop ( stopColor, 0.3 ) );
              stopColor.A = ( byte ) ( .12389 * color.A );
              gsc.Add ( new GradientStop ( stopColor, 0.5 ) );
              stopColor.A = ( byte ) ( .02654 * color.A );
              gsc.Add ( new GradientStop ( stopColor, 0.7 ) );
              stopColor.A = ( byte ) ( 0 );
              gsc.Add ( new GradientStop ( stopColor, 0.9 ) );
  
              gsc.Freeze ( );
  
              Brush [ ] colors = new Brush [ ]{
                  // TL
                  new RadialGradientBrush(gsc){ Center = new Point(1, 1), GradientOrigin = new Point(1, 1), RadiusX=1, RadiusY=1},
                  // T
                  new LinearGradientBrush(gsc, 0){ StartPoint = new Point(0,1), EndPoint=new Point(0,0)},
                  // TR
                  new RadialGradientBrush(gsc){ Center = new Point(0, 1), GradientOrigin = new Point(0, 1), RadiusX=1, RadiusY=1},
                  // R
                  new LinearGradientBrush(gsc, 0){ StartPoint = new Point(0,0), EndPoint=new Point(1,0)},
                  // BR
                  new RadialGradientBrush(gsc){ Center = new Point(0, 0), GradientOrigin = new Point(0, 0), RadiusX=1, RadiusY=1},
                  // B
                  new LinearGradientBrush(gsc, 0){ StartPoint = new Point(0,0), EndPoint=new Point(0,1)},
                  // BL
                  new RadialGradientBrush(gsc){ Center = new Point(1, 0), GradientOrigin = new Point(1, 0), RadiusX=1, RadiusY=1},
                  // L
                  new LinearGradientBrush(gsc, 0){ StartPoint = new Point(1,0), EndPoint=new Point(0,0)},
                  // C
                  new SolidColorBrush(color),
              };
  
              // This is a test pattern, uncomment to see how I'm drawing this
              //Brush[] colors = new Brush[]{
              //    Brushes.Red,
              //    Brushes.Green,
              //    Brushes.Blue,
              //    Brushes.Fuchsia,
              //    Brushes.Gainsboro,
              //    Brushes.LimeGreen,
              //    Brushes.Navy,
              //    Brushes.Orange,
              //    Brushes.White,
              //};
              double [ ] guidelineSetX = new double [ ] { transformed.X,
                                                      transformed.X+blurRadius,
                                                      transformed.Right-blurRadius,
                                                      transformed.Right};
  
              double [ ] guidelineSetY = new double [ ] { transformed.Y,
                                                      transformed.Y+blurRadius,
                                                      transformed.Bottom-blurRadius,
                                                      transformed.Bottom};
  
              drawingContext.PushGuidelineSet ( new GuidelineSet ( guidelineSetX, guidelineSetY ) );
              for ( int i = 0; i &lt; edges.Length; i++ )
              {
                  drawingContext.DrawRoundedRectangle ( colors [ i ], null, edges [ i ], 0.0, 0.0 );
              }
              drawingContext.Pop ( );
          }
  
          #endregion
  
      }
  }
  </code></pre>