title: C# WPF - Controlling column width of DataGrid ItemsSource items
body: <p>the issue is hard to explain but I will try my best. This code is part of a proof-of-concept for a webscraping application I am working on - only recently started.</p>
  <p>I am attempting to display 2 columns of data using a DataGrid. I am able to display the data by assigning an <code>ObservableCollection&lt;&gt;</code> to the <code>DataGrid.ItemsSource</code>(see image below), the issue I am getting is that I have no control over column properties. I am very new to WPF and have tried to 'sort of' adopt MVVM.</p>
  <p><a href="https://i.stack.imgur.com/4ODk0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4ODk0.png" alt="enter image description here" /></a></p>
  <p>In the picture it shows 4 columns, both the 'Title' and 'Price' from the ItemsSource are the 2 large ones.</p>
  <pre class="lang-cs prettyprint-override"><code>private void ScrapeProductButton_OnClick(object sender, RoutedEventArgs e)
  {
      string url = this.ProductToScrapeUrlTextBox.Text;
      scraper.ScrapeData(url);
      
      var entries = scraper.Entries;
      WebScrapedItems.ItemsSource = entries;
      
      WebScrapedItems.Columns.Add(new DataGridTextColumn(){Header = &quot;Title&quot;,
          Width = new DataGridLength(0.8, DataGridLengthUnitType.Star)});
      WebScrapedItems.Columns.Add(new DataGridTextColumn(){Header = &quot;Price&quot;,
          Width = new DataGridLength(0.2, DataGridLengthUnitType.Star)});
  }
  </code></pre>
  <p>As seen in the code behind above, I tried to add 2 columns of the correct formatting assuming the contents of the ItemsSource would just <em>do the same</em> (obviously it isn't). Turning AutoGeneratedColumns off in the XAML doesn't display the ItemsSource but does display the other columns correctly.</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;StackPanel&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
              &lt;ColumnDefinition Width=&quot;3*&quot;/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Button Name=&quot;ScrapeProductButton&quot; Grid.Column=&quot;0&quot;
              Margin=&quot;10 10 10 10&quot; Background=&quot;AntiqueWhite&quot;
              Content=&quot;Scrape Website&quot; FontWeight=&quot;Bold&quot;
              Click=&quot;ScrapeProductButton_OnClick&quot;/&gt;
          &lt;TextBox Name=&quot;ProductToScrapeUrlTextBox&quot; Grid.Column=&quot;1&quot;
              Margin=&quot;10&quot; Padding=&quot;2&quot;/&gt;
      &lt;/Grid&gt;
  
      &lt;DataGrid Height=&quot;350&quot; Width=&quot;740&quot; Margin=&quot;10&quot; Name=&quot;WebScrapedItems&quot;
          VerticalAlignment=&quot;Center&quot; HorizontalAlignment=&quot;Center&quot;
          RowHeight=&quot;30&quot; ColumnWidth=&quot;390&quot; IsReadOnly=&quot;True&quot;
          AutoGenerateColumns=&quot;True&quot; FrozenColumnCount=&quot;2&quot;&gt;
      &lt;/DataGrid&gt;
  
  &lt;/StackPanel&gt;
  </code></pre>
  <p>My goal is to just display the contents of the ItemsSource objects but such that they are correctly formatted. I feel like binding could be a potential reason its not working properly, but again I am still new to WPF and haven't started reading up on it yet.</p>
  <p>Any help would be greatly appreciated, and any WPF advice, MVVM or anything would be great also.</p>
  <p>Thanks!</p>