title: WPF Generate List of StackPanel with tag content "Blur"
body: <p>I need to iterate through a Grid which contains StackPanels, then I need to populate a list using location data from each StackPanel but only if that StackPanel has a tag with the content <code>Blur</code>. </p>
  
  <p>The code I have at the moment:</p>
  
  <pre><code> List&lt;XYWH&gt; MaskBlocks = new List&lt;XYWH&gt;();
              foreach (StackPanel gr in FindVisualChildren&lt;StackPanel&gt;(Container))
                  if (gr.Tag.ToString() == "Blur")
                      MaskBlocks.Add(new XYWH(TransformToAncestor(gr).Transform(new Point(0, 0)), new Point(gr.ActualWidth,gr.ActualHeight)));
  
              MessageBox.Show(MaskBlocks.ToArray().ToString());
  </code></pre>
  
  <p><code>FindVisualChildren</code></p>
  
  <pre><code>public static IEnumerable&lt;T&gt; FindVisualChildren&lt;T&gt;(DependencyObject depObj) where T : DependencyObject
          {
              if (depObj != null)
              {
                  for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(depObj); i++)
                  {
                      DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                      if (child != null &amp;&amp; child is T)
                      {
                          yield return (T)child;
                      }
  
                      foreach (T childOfChild in FindVisualChildren&lt;T&gt;(child))
                      {
                          yield return childOfChild;
                      }
                  }
              }
          }
  </code></pre>
  
  <p>I'm getting the following error when I attempt to add to the MaskBlocks List however. </p>
  
  <blockquote>
    <blockquote>
      <p>Additional information: The specified Visual is not an ancestor of this Visual.</p>
    </blockquote>
  </blockquote>
  
  <p>And the XYWH Class (basically XY Position and Width and Height):</p>
  
  <pre><code>public class XYWH
      {
          private Point _XY { get; set; }
          private Point _WH { get; set; }
  
          public XYWH(Point XY, Point WH)
          {
              _XY = XY;
              _WH = WH;
          }
      }
  </code></pre>
  
  <p>Edit: </p>
  
  <p>Trying to use a Rectangle, which should be used like so: <code>Rectangle(Point, Size)</code>, but getting the error saying <code>"Rectangle" does not contain a constructor that takes 2 arguments</code>.</p>
  
  <p>Code:</p>
  
  <pre><code>new Rectangle(gr.TransformToAncestor(this).Transform(new Point(0, 0)), new Size(gr.ActualWidth,gr.ActualHeight))
  </code></pre>