title: WPF DataGrid column ignores Width
body: <p>I have 2 DataGrids. To make them pretty I did some styling on them. I defined CellTemplate and HeaderTemplate and applied them in an implicit Style to the DataGrid. I add the coloumns in XAML and leave them be. They also have HeaderTemplates.</p>
  
  <p><strong>EDIT:</strong>
  I've tried some other scenarios, so here's some more on what happens:</p>
  
  <p>DataGrid 'A' works fine. It is initiated completely from XAML. It accepts all kinds of widths (including star size) and displays correctly. It is placed inside a UserControl that has a VM. Here's the XAML:</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding Items}"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Binding="{Binding Name}" Header="Name" MinWidth="50" Width="250" HeaderTemplate="{StaticResource DefaultDataGridHeader}"/&gt;
          &lt;DataGridTextColumn Binding="{Binding StartDate}" Header="Start"  MinWidth="50" Width="150" HeaderTemplate="{StaticResource DefaultDataGridHeader}" /&gt;
          &lt;DataGridTextColumn Binding="{Binding FinishDate}" Header="Finish"  MinWidth="50" Width="150" HeaderTemplate="{StaticResource DefaultDataGridHeader}" /&gt;
          &lt;DataGridTextColumn Binding="{Binding Leader}" Header="Leader"  MinWidth="50" Width="*" HeaderTemplate="{StaticResource DefaultDataGridHeader}" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Then there is DataGrid B...</p>
  
  <p>It's a little more complicated now. There's a Control that displays DataGrid B and does some other things. It is used in many other Views, with different data. So for every data there's another View, that contains the DataGrid columns. When the data arrives to the main control, it clears the columns of DataGrid B and repopulates it with the ones defined in the corresponding View.</p>
  
  <p>Here's DataGrid B in the usercontrol:</p>
  
  <pre><code>&lt;DataGrid x:Name="datagrid" ItemsSource="{Binding Items}" IsReadOnly="True"  SelectionMode="Single" /&gt;
  </code></pre>
  
  <p>The UserControl has a ObservableCollection <code>GridColumns</code> DependencyProp. This is where the actual DataGridColumns arrive. So on the CollectionChanged event I do this:</p>
  
  <pre><code>void GridColumnsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  {
      datagrid.Columns.Clear();
      foreach (var gridColumn in GridColumns)
      {
          datagrid.Columns.Add(gridColumn);
      }
  }
  </code></pre>
  
  <p>Here's an example for how I set the actual columns in data-specific View:</p>
  
  <pre><code>&lt;c:GenericList &gt;
      &lt;c:GenericList.GridColumns&gt;
              &lt;DataGridTextColumn Binding="{Binding Name}" Header="Name" 
              Width="300" HeaderTemplate="{StaticResource DefaultDataGridHeader}"/&gt;
      &lt;/c:GenericList.GridColumns&gt;
  &lt;/c:GenericList&gt;
  </code></pre>
  
  <p>-------- What happens with DataGrid B: --------</p>
  
  <ul>
  <li>If I leave the XAML like this, the column gets rendered with 300 px width. </li>
  <li>If I chnage the width to star size, or add any other columns with star size, they get rendered with 20px width. (even if there is some text in the header) </li>
  <li><p>If I set MinWidth, then all the star sized columns are rendered with MinWidth.</p></li>
  <li><p>If I leave out the whole 'placing columns from another view' part, and add some columns
  directly to datagrid then pixel size columns span to the width of the header text and star sized columns render with 20px width. Also if I resize a starsized column in the running app, some width constraints kick in and I cannot make the columns small again anymore. - But in this version, there's no data to bind to columns. Could that be causing this effect?</p></li>
  </ul>
  
  <p><strong>//end of EDIT.</strong></p>
  
  <p>The Styles were made in Blend with the "Edit a Copy" function.</p>
  
  <p>They are like this:</p>
  
  <pre><code>&lt;Style x:Key="DefaultDataGridColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}"&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridColumnHeader}"&gt;
                  &lt;Grid &gt;
                      &lt;Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                                                                     IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" 
                                                                     Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" 
                                                                     SeparatorVisibility="{TemplateBinding SeparatorVisibility}" Background="#00000000"&gt;
                          &lt;Grid&gt;
                              &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&gt;
                              &lt;Rectangle StrokeThickness="0" RadiusY="0" Opacity="0"&gt;
                                  &lt;Rectangle.Fill&gt;
                                      &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                                          &lt;GradientStop Color="#33000000" Offset="1"/&gt;
                                          &lt;GradientStop Color="#4CFFFFFF"/&gt;
                                          &lt;GradientStop Color="#007B7B7B" Offset="0.35"/&gt;
                                      &lt;/LinearGradientBrush&gt;
                                  &lt;/Rectangle.Fill&gt;
                              &lt;/Rectangle&gt;
                          &lt;/Grid&gt;
                      &lt;/Microsoft_Windows_Themes:DataGridHeaderBorder&gt;
                      &lt;Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}" Width="8"/&gt;
                      &lt;Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}" Width="8"/&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;DataTemplate x:Key="DefaultDataGridHeader"&gt;
      &lt;Border BorderBrush="{DynamicResource ShadowBrush}" Background="{x:Null}" d:DesignWidth="289" d:DesignHeight="72.28"&gt;
          &lt;Grid&gt;
              &lt;Rectangle RadiusY="0" StrokeThickness="0" Fill="{DynamicResource ActionTileBrush}" /&gt;
              &lt;Rectangle RadiusY="0" StrokeThickness="0" &gt;
                  &lt;Rectangle.Fill&gt;
                      &lt;SolidColorBrush Color="#88FFFFFF" /&gt;
                  &lt;/Rectangle.Fill&gt;
              &lt;/Rectangle&gt;
              &lt;TextBlock Text="{Binding}" Margin="12" FontSize="16" FontWeight="Bold"&gt;
                  &lt;TextBlock.Foreground&gt;
                      &lt;SolidColorBrush Color="#FF11789D" /&gt;
                  &lt;/TextBlock.Foreground&gt;
              &lt;/TextBlock&gt;
              &lt;Rectangle RadiusY="0" StrokeThickness="1" Stroke="{DynamicResource ShadowBrush}"&gt;&lt;/Rectangle&gt;
          &lt;/Grid&gt;
      &lt;/Border&gt;
  &lt;/DataTemplate&gt;
  
  
  &lt;Style x:Key="DefaultDataGridCellStyle" TargetType="{x:Type DataGridCell}"&gt;
      &lt;Setter Property="Background" Value="Transparent"/&gt;
      &lt;Setter Property="BorderBrush" Value="Transparent"/&gt;
      &lt;Setter Property="BorderThickness" Value="1"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt;
                  &lt;Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Padding="12,8"&gt;
                      &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
      &lt;Style.Triggers&gt;
          &lt;Trigger Property="IsSelected" Value="True"&gt;
              &lt;Setter Property="Background" Value="{DynamicResource HighlightBrush}"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="IsKeyboardFocusWithin" Value="True"&gt;
              &lt;Setter Property="BorderBrush" Value="{DynamicResource HighlightBrush}"/&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  
  
  
  &lt;Style x:Key="DefaultDataGridStyle" TargetType="{x:Type DataGrid}"&gt;
      &lt;Setter Property="AlternatingRowBackground" Value="{x:Null}"/&gt;
      &lt;Setter Property="AlternationCount" Value="2"/&gt;
      &lt;Setter Property="AutoGenerateColumns" Value="False"/&gt;
      &lt;Setter Property="Background" Value="{x:Null}"/&gt;
      &lt;Setter Property="BorderBrush" Value="{DynamicResource ChromeBrush}"/&gt;
      &lt;Setter Property="BorderThickness" Value="1"/&gt;
      &lt;Setter Property="CellStyle" Value="{StaticResource DefaultDataGridCellStyle}"/&gt;
      &lt;Setter Property="ColumnHeaderStyle" Value="{StaticResource DefaultDataGridColumnHeaderStyle}"/&gt;
      &lt;Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/&gt;
      &lt;Setter Property="HorizontalGridLinesBrush" Value="{DynamicResource ShadowBrush}"/&gt;
      &lt;Setter Property="RowBackground" Value="{DynamicResource BackgroundBrush}"/&gt;
      &lt;Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/&gt;
      &lt;Setter Property="ScrollViewer.CanContentScroll" Value="true"/&gt;
      &lt;Setter Property="ScrollViewer.PanningMode" Value="Both"/&gt;
      &lt;Setter Property="Stylus.IsFlicksEnabled" Value="True"/&gt;
      &lt;Setter Property="VerticalGridLinesBrush" Value="{DynamicResource ShadowBrush}"/&gt;
      &lt;Setter Property="VerticalContentAlignment" Value="Stretch"/&gt;
      &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt;
      &lt;Style.Triggers&gt;
          &lt;Trigger Property="IsGrouping" Value="true"&gt;
              &lt;Setter Property="ScrollViewer.CanContentScroll" Value="false"/&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Clearly there is something wrong somewhere and I should be able to set the columns to star size, or if set them to pixel size I expect them to keep that.</p>
  
  <p>Thank you for any help!</p>