title: WPF MeasureOverride infinite available size when used in Grid column with Width="Auto"?
body: <p>I'm making a <code>UserControl</code> which is supposed to be used inside a <code>Grid</code>. This <code>UserControl</code> should have a <code>Width</code> of either all the available space, or <code>100</code>, depending on a <code>bool</code>. I'm trying to accomplish this using <code>MeasureOverride</code> in my <code>UserControl</code>.</p>
  
  <p>The issue that I'm running into is the following: when my <code>UserControl</code> is placed inside a <code>Grid</code> column with <code>Width="Auto"</code>, the available <code>Size</code> that I get has an <code>Infinite</code> <code>Width</code>.</p>
  
  <p>I expected a <code>Width</code> of the <code>Grid</code> itself, minus any other fixed <code>Column</code> <code>Width</code>s etc.</p>
  
  <p>Example code:<br>
  <strong>Usercontrol XAML:</strong></p>
  
  <pre><code>&lt;UserControl x:Class="WpfApp1.UserControl1"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               mc:Ignorable="d"
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;Grid&gt;
          &lt;TextBox Text="Custom UserControl content" /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>UserControl codebehind:</strong></p>
  
  <pre><code>using System.Windows;
  
  namespace WpfApp1
  {
      public partial class UserControl1
      {
          public static readonly DependencyProperty ExpandProperty = DependencyProperty.Register(nameof(Expand), typeof(bool), typeof(UserControl1),
              new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.AffectsMeasure));
  
          public bool Expand
          {
              get =&gt; (bool)GetValue(ExpandProperty);
              set =&gt; SetValue(ExpandProperty, value);
          }
  
          public UserControl1()
          {
              InitializeComponent();
          }
  
          protected override Size MeasureOverride(Size constraint)
          {
              if (Expand)
              {
                  //constraint.Width is infinite, creates exception
                  return constraint;
              }
              return new Size(100, constraint.Height);
          }
      }
  }
  </code></pre>
  
  <p><strong>Main window XAML:</strong></p>
  
  <pre><code>&lt;Grid Background="White"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition MinWidth="200"/&gt;
          &lt;ColumnDefinition Width="Auto" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid Background="Beige" Grid.Column="0"&gt;
          &lt;CheckBox IsChecked="{Binding ElementName=MyControl,Path=Expand}" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;
              &lt;TextBlock Text="Expand MyControl"/&gt;
          &lt;/CheckBox&gt;
      &lt;/Grid&gt;
      &lt;wpfApp1:UserControl1 Grid.Column="1" x:Name="MyControl" Background="CornflowerBlue"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Is there any decent way to accomplish what I want? Or perhaps I should try to approach it in a different way?</strong></p>