DataGrid column headers do not match data

I have a DataGrid that is pretty simple compared to DataGrids. For some reason, the headers do not match the rest of the data, as shown in the screenshot below:

enter image description here

I searched the internet but cannot 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's also worth noting that when I click on one of the Combobox cells, the headers are aligned correctly.

+4
source share
4 answers

You definitely have a style or something that hides the top left Select All button in the datagrid. Therefore, the columns are slightly shifted to the left.

Use this thread to get this button in the DataGrid.OnLoad and check its Visibility property.

Select all WPF DataGrid buttons

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

+3
source

Finally, I defeated this problem. Find a solution here .

Sorry, I didn’t notice that you are not overriding the DataGrid control template. I'm afraid you will have to define a control pattern to fix the behavior of the DataGrid.

PS: I have .NET Framework 4.0

+2
source

I found that just setting HeadersVisiblity to "Column" does the trick - see XAML below.
Easier than mastering this SelectAll Button ...

 <DataGrid x:Name="myGrid" HeadersVisibility="Column"> 
+1
source

You can use the DataGrid Like property and also define a specific css class for the row

 HeaderStyle-HorizontalAlign="" HeaderStyle-CssClass="" 
0
source

Source: https://habr.com/ru/post/1442981/


All Articles