title: How to make DataGrid column stretch from C# code?
body: <p>I'm creating the columns of a WPF <code>DataGrid</code> in my C# code. I want one of the columns to stretch to the width of the <code>DataGrid</code> automatically. In XAML, I'd set <code>Width="*"</code>. How do I do it in code?</p>
  
  <p><strong>EDIT</strong><br>
  Some of the answers seem to lead me to the right solution, but I feel I need to elaborate a bit more what I'm trying to do:</p>
  
  <p>I'm actually deriving a new class from <code>DataGrid</code>. In its constructor, I add four columns like that:</p>
  
  <pre><code>this.IsReadOnly = true;
  this.AutoGenerateColumns = false;
  this.ItemsSource = m_dataSource;
  
  DataGridTextColumn anColumn = new DataGridTextColumn() { Header = "Col1", Binding = new Binding("B1") };
  DataGridTextColumn tpColumn = new DataGridTextColumn() { Header = "Col2", Binding = new Binding("B2") };
  DataGridTextColumn txColumn = new DataGridTextColumn() { Header = "Col3", Binding = new Binding("B3") };
  DataGridTextColumn mdColumn = new DataGridTextColumn() { Header = "Col4", Binding = new Binding("B4") };
  
  this.Columns.Add(anColumn);
  this.Columns.Add(tpColumn);
  this.Columns.Add(txColumn);
  this.Columns.Add(mdColumn);
  </code></pre>
  
  <p>I tried setting the last column's width like <em>user24601</em> suggested:</p>
  
  <pre><code>mdColumn.Width = new DataGridLength(0.5, DataGridLengthUnitType.Star);
  </code></pre>
  
  <p>But this creates a column that is so wide I can scroll and scroll for a <em>very</em> long time... Same problem when I use <code>0.1</code> or even smaller values.</p>
  
  <p>I seems to me I'm doing it in the wrong place somehow?</p>
  
  <p><strong>EDIT 2</strong><br>
  OK, I may have the problem because I'm actually adding this to a <code>ScrollViewer</code>. I'll run some further tests first...</p>
  
  <p><strong>EDIT 3</strong><br>
  Well, things don't work when having the <code>DataGrid</code> within a <code>ScrollViewer</code>... When I remove the <code>ScrollViewer</code>, things work like <em>user24601</em> said.</p>