Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid column headers not aligned with data

Tags:

c#

.net

mvvm

wpf

xaml

I've got a DataGrid, which is quite simple as far as DataGrids go. For some reason or other, the headers are not aligned with the rest of the data, as shown in the screenshot below:

enter image description here

I've searched the internet but cannot seem to find a solution for it. Here is my DataGrid code:

Grid>
        <DataGrid Name="dgAttributes" 
                  ItemsSource="{Binding itemsSource}" 
                  AutoGenerateColumns="False" 
                  CanUserAddRows="False" 
                  CanUserDeleteRows="False" 
                  CanUserReorderColumns="False" 
                  CanUserResizeColumns="False" 
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  >
            <DataGrid.Columns>
                <DataGridTextColumn Width="Auto" IsReadOnly="True" Binding="{Binding Field}" Header="Fields"/>
                <DataGridComboBoxColumn  Width="95" IsReadOnly="False" Header="Order" ItemsSource="{Binding Source={StaticResource SortOrderProvider}}" SelectedItemBinding="{Binding SortBy, Mode=TwoWay}"/>
                <DataGridCheckBoxColumn Width="Auto" IsReadOnly="False" Binding="{Binding GroupBy}" Header="Group By"/>
                <DataGridComboBoxColumn Width="85" IsReadOnly="False" Header="Aggregate" ItemsSource="{Binding Source={StaticResource AggregateProvider}}" SelectedItemBinding="{Binding AggregateBy, Mode=TwoWay}"/>
                <DataGridTextColumn Width="Auto" IsReadOnly="False" Binding="{Binding Having}" Header="Having"/>
                <DataGridTextColumn Width="Auto" IsReadOnly="False" Binding="{Binding DisplayOrder}" Header="Display Order"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

It may also be worth mentioning that when I click on one of the Combobox cells, the headers align themselves properly.

like image 900
Dot NET Avatar asked Oct 30 '12 13:10

Dot NET


2 Answers

You are definitely having some style or something that is hiding the top left Select All button of datagrid. Hence the columns are shifted to left a little.

Use this thread to get hold of that button in DataGrid.OnLoad and check its Visibility property.

Select All button WPF DataGrid

If its collpased/hidden, set the visibility to Visbility.Visible. Or check its Width being zero and set appropriate Width.

like image 68
WPF-it Avatar answered Sep 19 '22 15:09

WPF-it


Finally I defeated this problem Find the solution here.

Sorry I did not notice you are not overriding the DataGrid control template. I am afraid , you will have to define a control template in order to correct the DataGrid behavior.

PS : I have .NET Framework 4.0

like image 22
Ashi Avatar answered Sep 21 '22 15:09

Ashi