title: How to override MeasureOverride to find the size of ItemsControl
body: <p>I am developing a UserControl that consists of a block with a heading and a list of items (as <code>ItemsControl</code>). The usercontrol is added dynamically to a canvas. I need to get the actual size of the control (including space taken by <code>ItemsControl</code>) before it gets rendered. I tried overriding <code>MeasureOverride</code> method of the UserControl hoping that the size would be reflected in <code>DesiredSize</code> property. But it is not working.</p>
  
  <p>The XAML is:</p>
  
  <pre><code>&lt;UserControl x:Class="MyTools.MyControl"
         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"
         mc:Ignorable="d"
         DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;    
      &lt;Grid x:Name="LayoutRoot" Background="White"&gt;
          &lt;Border Name="MainBorder" CornerRadius="5" BorderThickness="2" BorderBrush="Black"&gt;
          &lt;Grid  Name="grid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" &gt;
             &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="34" /&gt;
                  &lt;RowDefinition Height="*" /&gt;
             &lt;/Grid.RowDefinitions&gt;
             &lt;Grid Name="titleGrid" Grid.Row="0" Background="#FF727272"&gt;
                &lt;TextBlock Name="titleText" HorizontalAlignment="Center" Text="{Binding ControlName}" VerticalAlignment="Center" FontSize="13" FontWeight="Bold" Foreground="Beige" /&gt;
              &lt;/Grid&gt;
             &lt;Grid Name="gridpr" Grid.Row="1" Background="#12C48F35"&gt;
             &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="*" /&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Border Name="borderPr"  CornerRadius="3" Margin="10"  BorderThickness="1" BorderBrush="LightGray" Grid.Row="0"&gt;
                  &lt;Grid Name="gridPr" Background="#FFC1C1C1" MouseLeftButtonUp="gridPr_MouseLeftButtonUp"&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock  HorizontalAlignment="Center" Name="txtPr" Text="SubItems" VerticalAlignment="Center" Foreground="#FF584848" FontSize="12" /&gt;
                          &lt;ItemsControl x:Name="pitems" ItemsSource="{Binding MyItems}" &gt;
                              &lt;ItemsControl.ItemTemplate&gt;
                              &lt;DataTemplate&gt;
                              &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="15,0,0,0"&gt;
                                  &lt;TextBlock Text="{Binding MyVal}" /&gt;
                              &lt;/StackPanel&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/ItemsControl.ItemTemplate&gt;
                          &lt;/ItemsControl&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Grid&gt;
              &lt;/Border&gt;
          &lt;/Grid&gt;
          &lt;/Grid&gt;
          &lt;/Border&gt;
      &lt;/Grid&gt;
   &lt;/UserControl&gt;   
  </code></pre>
  
  <p>I am overriding MeasureOverride of the UserControl as shown below:</p>
  
  <pre><code> namespace MyTools
   {
     public partial class MyControl : UserControl
     {
         public MyControl()
         {
             InitializeComponent();
         }
  
         public string ControlName { get; set; }
         public object MyItems { get; set; }
  
         public class Row
         {
             public string MyVal { get; set; }
         }
  
         protected override Size MeasureOverride(Size availableSize)
         {
             var desiredSize = base.MeasureOverride(availableSize);
             var sideLength = Math.Min(desiredSize.Width, desiredSize.Height);
  
             desiredSize.Width = sideLength;
             desiredSize.Height = sideLength;
  
             return desiredSize;
         }
       }
     }
  </code></pre>
  
  <p>Client Code:</p>
  
  <pre><code>       MyControl control1 = new MyControl();          
         control1.ControlName = "Test Name";
  
         var test = new List&lt;MyControl.Row&gt;(
           new MyControl.Row[]
             {
                 new MyControl.Row {MyVal = "Item1"},
                 new MyControl.Row {MyVal = "Item2"},
                 new MyControl.Row {MyVal = "Item3"}
             });
  
         control1.MyItems = test;
  
          control1.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
          MessageBox.Show(control1.DesiredSize.Height.ToString());
          canvas1.Children.Add(control1);
  </code></pre>
  
  <p>I am not getting the actual size using the <code>DesiredSize.Height</code> property from the client. Any idea on how to fix this?</p>