title: Column Headers Offset From Data in Wpf Datagrid
body: <p>Good morning</p>
  
  <p>I have a Wpf datagrid that is displaying an observable collection of a custom type</p>
  
  <p>I group the data using a collection view source in XAML on two seperate properties, and I have styled the groups to display as expanders.</p>
  
  <p>For clarity, as there is a lot of data I feel I have to use margins and spacing otherwise things look very cluttered.</p>
  
  <p>My problem is that with two levels of hierarchical expanders the column data is now substantially offset from the column headers meaning that they do not properly line up.</p>
  
  <p>I have tried several thing, like setting the margin of the column headers and the width (both actual and normal). However all of my attempts end up resizing the whole column so that the offset stays the same but the columns move.</p>
  
  <p>so my question:</p>
  
  <p><strong>How can I change the visible width or offset of a column header to ensure that the headers line up with the data</strong></p>
  
  <ul>
  <li>Visual Studio 2012</li>
  <li>Wpf</li>
  <li>C#</li>
  <li>DataGrid</li>
  </ul>
  
  <p><strong>EDIT This is what I mean</strong></p>
  
  <p><img src="https://i.stack.imgur.com/pE7xq.png" alt="Offset Columns"></p>
  
  <p><strong>EDIT 2 - MY Xaml for Grouping</strong></p>
  
  <pre><code>&lt;!--  Style for groups at top level.  --&gt;
  &lt;GroupStyle&gt;
      &lt;GroupStyle.ContainerStyle&gt;
          &lt;Style TargetType="{x:Type GroupItem}"&gt;
              &lt;Setter Property="Margin" Value="0" /&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt;
                          &lt;Expander Margin="5,10,5,5"
                                    BorderBrush="{StaticResource BlackBrush}"
                                    BorderThickness="1"
                                    Header="{Binding Name}"
                                    IsExpanded="True"&gt;
  
                              &lt;Expander.Template&gt;
                                  &lt;!--  The basic expander  --&gt;
                                  &lt;ControlTemplate TargetType="{x:Type Expander}"&gt;
                                      &lt;!--  Put a border around the expander  --&gt;
                                      &lt;Border Background="{Binding Path=Name,
                                                                   Converter={StaticResource ColourConverter}}"
                                              BorderBrush="{StaticResource GreyBrush}"
                                              BorderThickness="2"
                                              CornerRadius="3"&gt;
  
                                          &lt;!--  Use a dock panel so that the toggle button is docked to the top and the content is docked to the bottom  --&gt;
                                          &lt;DockPanel Margin="0"&gt;
  
                                              &lt;!--  Add the toggle button  --&gt;
                                              &lt;ToggleButton x:Name="ExpanderButton"
                                                            Margin="0"
                                                            Content="{TemplateBinding Header}"
                                                            DockPanel.Dock="Top"
                                                            FontSize="14"
                                                            FontWeight="Bold"
                                                            Foreground="{StaticResource BlackBrush}"
                                                            IsChecked="{Binding Path=IsExpanded,
                                                                                RelativeSource={RelativeSource TemplatedParent}}"
                                                            OverridesDefaultStyle="True"
                                                            Template="{StaticResource AnimatedExpanderButton}" /&gt;
  
                                              &lt;ContentPresenter x:Name="ExpanderContent"
                                                                Margin="5"
                                                                ContentSource="Content"
                                                                DockPanel.Dock="Bottom"
                                                                Visibility="{Binding ElementName=ExpanderButton,
                                                                                     Path=IsChecked,
                                                                                     Converter={StaticResource VisibilityConverter}}" /&gt;
                                          &lt;/DockPanel&gt;
                                      &lt;/Border&gt;
                                  &lt;/ControlTemplate&gt;
                              &lt;/Expander.Template&gt;
                              &lt;ItemsPresenter /&gt;
                          &lt;/Expander&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/GroupStyle.ContainerStyle&gt;
  &lt;/GroupStyle&gt;
  
  &lt;!--  Style for groups under the top level.  --&gt;
  &lt;GroupStyle&gt;
      &lt;GroupStyle.ContainerStyle&gt;
          &lt;Style TargetType="{x:Type GroupItem}"&gt;
  
              &lt;Setter Property="Margin" Value="0,0,0,5" /&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt;
                          &lt;Expander Margin="5"
                                    Background="{Binding Path=Name,
                                                         Converter={StaticResource ColourConverter}}"
                                    IsExpanded="True"
                                    Visibility="{Binding Items[0].IsSelectedInSidebar,
                                                         Converter={StaticResource VisibilityConverter}}"&gt;
  
                              &lt;Expander.Template&gt;
                                  &lt;!--  The basic expander  --&gt;
                                  &lt;ControlTemplate TargetType="{x:Type Expander}"&gt;
                                      &lt;!--  Put a border around the expander  --&gt;
                                      &lt;Border Background="{Binding Path=Name,
                                                                   Converter={StaticResource ColourConverter}}"
                                              BorderBrush="{StaticResource GreyBrush}"
                                              BorderThickness="2"
                                              CornerRadius="3"&gt;
  
                                          &lt;!--  Use a dock panel so that the toggle button is docked to the top and the content is docked to the bottom  --&gt;
                                          &lt;DockPanel Margin="0"&gt;
  
                                              &lt;!--  Add the toggle button  --&gt;
                                              &lt;ToggleButton x:Name="ExpanderButton"
                                                            Content="{Binding Path=Name}"
                                                            DockPanel.Dock="Top"
                                                            FontSize="12"
                                                            IsChecked="{Binding Path=IsExpanded,
                                                                                RelativeSource={RelativeSource TemplatedParent}}"
                                                            OverridesDefaultStyle="True"
                                                            Template="{StaticResource AnimatedExpanderButton}" /&gt;
  
                                              &lt;ContentPresenter x:Name="ExpanderContent"
                                                                Margin="5"
                                                                ContentSource="Content"
                                                                DockPanel.Dock="Bottom"
                                                                Visibility="{Binding ElementName=ExpanderButton,
                                                                                     Path=IsChecked,
                                                                                     Converter={StaticResource VisibilityConverter}}" /&gt;
                                          &lt;/DockPanel&gt;
                                      &lt;/Border&gt;
                                  &lt;/ControlTemplate&gt;
                              &lt;/Expander.Template&gt;
                              &lt;Expander.Content&gt;
                                  &lt;Border BorderBrush="{StaticResource BlackBrush}" BorderThickness="1"&gt;
                                      &lt;Grid&gt;
                                          &lt;Grid.RowDefinitions&gt;
                                              &lt;RowDefinition Height="*" /&gt;
                                              &lt;RowDefinition Height="Auto" /&gt;
                                          &lt;/Grid.RowDefinitions&gt;
                                          &lt;ItemsPresenter Grid.Row="0" Margin="0" /&gt;
  
                                          &lt;Border Grid.Row="1"
                                                  Margin="0,10,0,0"
                                                  BorderBrush="{StaticResource BlackBrush}"
                                                  BorderThickness="0,1,0,0"
                                                  Visibility="{Binding Data.SettingRepository.MainDataSummaryVisible,
                                                                       Source={StaticResource BindingProxy},
                                                                       Converter={StaticResource VisibilityConverter}}"&gt;
                                              &lt;Grid Background="{StaticResource WhiteBrush}"&gt;
                                                  &lt;Grid.RowDefinitions&gt;
                                                      &lt;RowDefinition Height="Auto" /&gt;
                                                      &lt;RowDefinition Height="Auto" /&gt;
                                                      &lt;RowDefinition Height="Auto" /&gt;
                                                  &lt;/Grid.RowDefinitions&gt;
  
                                                  &lt;Grid.ColumnDefinitions&gt;
                                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                                      &lt;ColumnDefinition Width="Auto" /&gt;
                                                  &lt;/Grid.ColumnDefinitions&gt;
  
                                                  &lt;Grid Grid.Row="0" Grid.ColumnSpan="6"&gt;
                                                      &lt;Grid.ColumnDefinitions&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                          &lt;ColumnDefinition Width="Auto" /&gt;
                                                      &lt;/Grid.ColumnDefinitions&gt;
  
                                                      &lt;TextBlock Grid.Column="0"
                                                                 Margin="5"
                                                                 FontWeight="Bold"
                                                                 Text="{Binding Path=Items[0].Option1Title}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
                                                      &lt;TextBlock Grid.Column="1"
                                                                 Margin="5"
                                                                 Text="{Binding Path=Items[0].Option1Data,
                                                                                Mode=OneWay}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
  
                                                      &lt;TextBlock Grid.Column="2"
                                                                 Margin="5"
                                                                 FontWeight="Bold"
                                                                 Text="{Binding Path=Items[0].Option2Title}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
                                                      &lt;TextBlock Grid.Column="3"
                                                                 Margin="5"
                                                                 Text="{Binding Path=Items[0].Option2Data,
                                                                                Mode=OneWay}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
                                                      &lt;TextBlock Grid.Column="4"
                                                                 Margin="5"
                                                                 FontWeight="Bold"
                                                                 Text="{Binding Path=Items[0].Option3Title}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
                                                      &lt;TextBlock Grid.Column="5"
                                                                 Margin="5"
                                                                 Text="{Binding Path=Items[0].Option3Data,
                                                                                Mode=OneWay}" /&gt;
                                                      &lt;TextBlock Grid.Column="6"
                                                                 Margin="5"
                                                                 FontWeight="Bold"
                                                                 Text="{Binding Path=Items[0].Option4Title}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
                                                      &lt;TextBlock Grid.Column="7"
                                                                 Margin="5"
                                                                 Text="{Binding Path=Items[0].Option4Data,
                                                                                Mode=OneWay}"
                                                                 Visibility="{Binding Data.SettingRepository.MainDataShowSampleOptions,
                                                                                      Source={StaticResource BindingProxy},
                                                                                      Converter={StaticResource VisibilityConverter}}" /&gt;
  
                                                      &lt;TextBlock Grid.Column="8"
                                                                 Margin="5"
                                                                 FontWeight="Bold"
                                                                 Text="{x:Static languages:Strings.SampleIsAnnealedColumnHeader}" /&gt;
                                                      &lt;CheckBox Grid.Column="9"
                                                                Margin="3,5,5,5"
                                                                IsChecked="{Binding Path=Items[0].SampleIsAnnealed,
                                                                                    Mode=OneWay}"
                                                                IsHitTestVisible="False"
                                                                Style="{StaticResource FandFCheckBox}" /&gt;
                                                  &lt;/Grid&gt;
  
                                                  &lt;!--  The mean Match temperature  --&gt;
                                                  &lt;TextBlock Grid.Row="1"
                                                             Grid.Column="0"
                                                             Margin="5"
                                                             FontWeight="Bold"
                                                             Text="{x:Static languages:Strings.MeanSampleMatchTemperatureTitle}" /&gt;
                                                  &lt;TextBlock Grid.Row="1"
                                                             Grid.Column="1"
                                                             Margin="5"
                                                             Text="{Binding Path=Items[0].SampleMeanMatchTemperature,
                                                                            Mode=OneWay,
                                                                            StringFormat=\{0:N2\}}" /&gt;
  
                                                  &lt;!--  The match temperature range  --&gt;
                                                  &lt;TextBlock Grid.Row="1"
                                                             Grid.Column="2"
                                                             Margin="5"
                                                             FontWeight="Bold"
                                                             Text="{x:Static languages:Strings.SampleTemperatureRangeTitle}" /&gt;
                                                  &lt;TextBlock Grid.Row="1"
                                                             Grid.Column="3"
                                                             Margin="5"
                                                             Text="{Binding Path=Items[0].SampleMatchTemperatureRange}" /&gt;
  
                                                  &lt;!--  The match temperature standard deviation  --&gt;
                                                  &lt;TextBlock Grid.Row="1"
                                                             Grid.Column="4"
                                                             Margin="5"
                                                             FontWeight="Bold"
                                                             Text="{x:Static languages:Strings.SampleTemperatureStandardDeviationTitle}" /&gt;
                                                  &lt;TextBlock Grid.Row="1"
                                                             Grid.Column="5"
                                                             Margin="5"
                                                             Text="{Binding Path=Items[0].SampleMatchTemperatureStandardDeviation,
                                                                            Mode=OneWay,
                                                                            StringFormat=\{0:N3\}}" /&gt;
  
                                                  &lt;!--  The mean refractive index  --&gt;
                                                  &lt;TextBlock Grid.Row="2"
                                                             Grid.Column="0"
                                                             Margin="5"
                                                             FontWeight="Bold"
                                                             Text="{x:Static languages:Strings.SampleMeanRefractiveIndexTitle}" /&gt;
                                                  &lt;TextBlock Grid.Row="2"
                                                             Grid.Column="1"
                                                             Margin="5"
                                                             Text="{Binding Path=Items[0].SampleMeanRefractiveIndex,
                                                                            Mode=OneWay,
                                                                            StringFormat=\{0:N5\}}" /&gt;
  
                                                  &lt;!--  The refractive index range  --&gt;
                                                  &lt;TextBlock Grid.Row="2"
                                                             Grid.Column="2"
                                                             Margin="5"
                                                             FontWeight="Bold"
                                                             Text="{x:Static languages:Strings.SampleRIRangeTitle}" /&gt;
                                                  &lt;TextBlock Grid.Row="2"
                                                             Grid.Column="3"
                                                             Margin="5"
                                                             Text="{Binding Path=Items[0].SampleRefractiveIndexRange}" /&gt;
  
                                                  &lt;!--  The refractive index standard deviation  --&gt;
                                                  &lt;TextBlock Grid.Row="2"
                                                             Grid.Column="4"
                                                             Margin="5"
                                                             FontWeight="Bold"
                                                             Text="{x:Static languages:Strings.SampleRIStandardDeviationTitle}" /&gt;
                                                  &lt;TextBlock Grid.Row="2"
                                                             Grid.Column="5"
                                                             Margin="5"
                                                             Text="{Binding Path=Items[0].SampleRefractiveIndexStandardDeviation,
                                                                            Mode=OneWay,
                                                                            StringFormat=\{0:N7\}}" /&gt;
                                              &lt;/Grid&gt;
                                          &lt;/Border&gt;
                                      &lt;/Grid&gt;
                                  &lt;/Border&gt;
                              &lt;/Expander.Content&gt;
                          &lt;/Expander&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/GroupStyle.ContainerStyle&gt;
  &lt;/GroupStyle&gt;
  </code></pre>