title: C# WPF) DropShadowEffect is sometimes missing when dynamically adding a control
body: <p><a href="https://i.stack.imgur.com/IzUqV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IzUqV.png" alt="enter image description here"></a>  </p>
  
  <pre><code>foreach (DataRow dr in dt.Rows)
  {
      Rectangle rectangle_timeline = new Rectangle();
      rectangle_timeline.Height = 19;
      rectangle_timeline.Cursor = Cursors.Hand;
  
      rectangle_timeline.Effect = new DropShadowEffect
      {
          Color = new Color { A = 255, R = 0, G = 0, B = 0 },
          Direction = 315,
          ShadowDepth = 5,
          Opacity = 1
      };
  
      Grid_Timeline.Children.Add(rectangle_timeline);
  }
  </code></pre>
  
  <p>I dynamically add a Rectangle with above simple code as shown image.</p>
  
  <p>However, sometimes, randomly, there're rectangles without DropShadowEffect like yellow rectangles and 1 blue rectangle at the lowest.</p>
  
  <p>As you see the code, if a rectangle is added, the code for DropShadowEffect should have to be worked.</p>
  
  <p>I'm wondering why this is happening.</p>
  
  <p>Thank you !</p>
  
  <p>XAML code added-</p>
  
  <pre><code>&lt;Grid x:Name="Grid_Timeline" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True" Width="1159" HorizontalAlignment="Left" VerticalAlignment="Top" SnapsToDevicePixels="True"&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Minimal code to re-produce is added-</p>
  
  <pre><code>private void Window_Loaded(object sender, RoutedEventArgs e)
  {
          int count_each_category = 0;
          string current_therapeutic_category = String.Empty;
  
          foreach (DataRow dr_test in dt.Rows)
          {
              if (dr_test != null)
              {
                  if (current_category == String.Empty)
                  {
                      count_each_category++;
  
                      current_category = dr_test["category"].ToString();
                  }
                  else
                  {
                      if (current_category == dr_test["category"].ToString())
                      {
                          // empty
                      }
                      else
                      {
                          count_each_category++;
  
                          current_category = dr_test["category"].ToString();
                      }
                  }
  
                  Rectangle rectangle_test = new Rectangle();
  
                  rectangle_test.HorizontalAlignment = HorizontalAlignment.Left;
                  rectangle_test.VerticalAlignment = VerticalAlignment.Top;
  
                  rectangle_test.Margin = new Thickness(119 + (((Convert.ToDateTime(dr_test["date"]) - DateTime.Today.AddYears(-10)).TotalDays) * 0.27), count_each_category * 50, 0, 0);
  
                  rectangle_test.Width = 0.27 * Convert.ToInt32(dr_test["howlong"]);
                  rectangle_test.Height = 19;
  
                  rectangle_test.Effect = new DropShadowEffect
                  {
                      Color = new Color { A = 255, R = 0, G = 0, B = 0 },
                      Direction = 315,
                      ShadowDepth = 5,
                      Opacity = 1
                  };
  
                  rectangle_test.Fill = Brushes.LightGreen;
  
                  Grid_Timeline.Children.Add(rectangle_test);
              }
          }
  }
  </code></pre>
  
  <p>XAML code of WPF Window</p>
  
  <pre><code>&lt;Window x:Class="OperationSWdummy.Green_Timeline"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:OperationSWdummy"
      mc:Ignorable="d"
      Title="Green_Timeline" Width="1165" Background="White" ResizeMode="CanMinimize" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True" SnapsToDevicePixels="True" Loaded="Window_Loaded"&gt;
  &lt;Grid x:Name="Grid_Timeline" ScrollViewer.VerticalScrollBarVisibility="Auto" UseLayoutRounding="True" Width="1159" HorizontalAlignment="Left" VerticalAlignment="Top" SnapsToDevicePixels="True"&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Raw data of dt (DataTable)</p>
  
  <pre><code>date        datetime
  
  category    NVARCHAR
  
  howlong     int
  
     date     category howlong 
  2015-01-25    HHH      60 
  2014-04-03    AAA      60 
  2015-01-25    DDD      60 
  2014-04-03    UUU      60 
  2015-01-25    CCC      60 
  2015-11-07    PPP      30 
  2015-01-25    TTT      60 
  2015-11-07    MMM      30 
  2015-02-22    MMM      30 
  2015-11-07    VVV       8 
  </code></pre>
  
  <p>Result of above minimal code
  <a href="https://i.stack.imgur.com/btzd6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/btzd6.png" alt="Result of above minimal code"></a></p>
  
  <p>Another minimal code to create rectangles randomly-</p>
  
  <pre><code>for (int k = 0; k &lt; 191; k++)
          {
              Rectangle rec_test = new Rectangle();
              rec_test.Margin = new Thickness(random_margin.Next(100, 1000), 29 * (k % 10), 0, 0);
              rec_test.Width = random_width.Next(10, 40);
              rec_test.HorizontalAlignment = HorizontalAlignment.Left;
              rec_test.VerticalAlignment = VerticalAlignment.Top;
              rec_test.Height = 14;
              rec_test.Cursor = Cursors.Hand;
              rec_test.Effect = new DropShadowEffect
              {
                  Color = new Color { A = 255, R = 0, G = 0, B = 0 },
                  Direction = 315,
                  ShadowDepth = 5,
                  Opacity = 1
              };
  
              rec_test.Fill = Brushes.Green;
  
              Grid_Timeline.Children.Add(rec_test);
          }
  </code></pre>