title: Why is UIElement.DesiredSize different when UIElement.Parent is null and not null
body: <p>Why is UIElement.DesiredSize different when UIElement.Parent is null and not null</p>
  <p>I need to calculate the dimensions of elements that are not on the form.
  I am using code</p>
  <pre><code>       Element = new Element()
         Element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
         Size result = Element.DesiredSize;
  </code></pre>
  <p>But I get different results when Element.Parent = null and when
  Element.Parent != null</p>
  <p>Properties UseLayoutRounding, SnapsToDevicePixels don't help</p>
  <p>Here is a complete example</p>
  <pre><code>    private void Button_Click_1(object sender, RoutedEventArgs e)
      {
          TextBlock cell1 = CreateTextBlock();
          cell1.InvalidateMeasure();
          //VisualTreeHelperEh.ForAllChildren(cell1, true, true, (lmt) =&gt; lmt.InvalidateMeasure());
          cell1.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
          Size result1 = cell1.DesiredSize;
          double height1 = cell1.DesiredSize.Height;
          Debug.WriteLine(&quot;height1=&quot; + height1.ToString());
      
          TextBlock cell2 = CreateTextBlock();
          cell2.UseLayoutRounding = true;
          cell2.SnapsToDevicePixels = true;
          //VisualTreeHelperEh.ForAllChildren(cell2, true, true, (lmt) =&gt; lmt.InvalidateMeasure());
          cell2.InvalidateMeasure();
          cell2.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
          Size result2 = cell2.DesiredSize;
          double height2 = cell2.DesiredSize.Height;
          Debug.WriteLine(&quot;height2=&quot; + height2.ToString());
      
          TextBlock cell3 = CreateTextBlock(); 
          InWindowGrid.Children.Add(cell3);
          cell3.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
          //cell3.Parent;
          Size result3 = cell3.DesiredSize;
          double height3 = cell3.DesiredSize.Height;
          Debug.WriteLine(&quot;height3=&quot; + height3.ToString());
      }
      
      private TextBlock CreateTextBlock()
      {
          TextBlock result = new TextBlock();
          result.Text = &quot;Test Wg&quot;;
          result.Padding = new Thickness(2, 1, 2, 1);
          result.VerticalAlignment = VerticalAlignment.Top;
          result.HorizontalAlignment = HorizontalAlignment.Left;
          return result;
      }
  </code></pre>
  <p>Result</p>
  <pre><code>height1 = 17,96
  height2 = 18
  height3 = 17
  </code></pre>