title: How can I set the width of a DataGridColumn to fit contents ("Auto"), but completely fill the available space for the DataGrid in MVVM?
body: <p>I have a WPF <code>DataGrid</code> that contains some data. I would like to set the width of the columns such that the content fits in and never gets cropped (instead, a horizontal scroll bar should become visible). Additionally, I want the <code>DataGrid</code> to fill the whole place available (I am working with a <code>DockPanel</code>). I am using the following code (simplified):</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding Table}"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn MinWidth="100" Width="Auto" Header="Column 1" Binding="{Binding Property1}" /&gt;
          &lt;DataGridTextColumn MinWidth="200" Width="Auto" Header="Column 2" Binding="{Binding Property2}" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>This apparently does not work out of the box with <code>Width="Auto"</code> as it always looks something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/KmMR2.png" alt="DataGrid: only the first part of the row is marked as &quot;selected&quot;"></p>
  
  <p>This obviously looks ugly. I would like to have the whole row selected, or, which would be much better, the columns to fill the whole width, but as one can see, this does not work.</p>
  
  <p>If I use <code>Width="*"</code> instead, the content of the columns gets cropped which is even worse for me.</p>
  
  <p>I found a <a href="https://stackoverflow.com/questions/3404067/wpf-datagrid-column-with-width-but-minwidth-to-fit-contents">similar question here</a>, and a workaround was posted there. This may work, but I am working with the MVVM pattern, so the <strong>ItemsSource gets updated in the ViewModel</strong> and I cannot think about a way of doing it from there, because I cannot access the <code>ActualWidth</code> property of the <code>DataGridColumn</code>. Also, I would like to do it only in XAML if possible.</p>
  
  <p>I would appreciate any help. Thanks!</p>
  
  <p>Edit: As I still don't have a clue what to do about it, I start a small bounty. I would be very happy about a suggestion what one could do about my problem. Thanks again!</p>
  
  <p>Edit 2: After saus' answer I thought about the options again. The problem is that I need to update the <code>Width</code> and the <code>MinWidth</code> properties also during the application is running, so not only after loading the window. I already tried to do something like</p>
  
  <pre><code>column.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
  column.MinWidth = column.ActualWidth;
  column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
  </code></pre>
  
  <p>in some event that is fired when the underlying <code>ItemsSource</code> of the <code>DataGrid</code> is updating. However, this does not work, as the <code>ActualWidth</code> property does not seem to change after setting the <code>Width</code> on <code>Auto</code>. Is there an option to somehow "repaint" it in order to get the <code>ActualWidth</code> property updated? Thanks!</p>