DataGrid 列标题未与数据对齐

Dot*_*NET 5 .net c# wpf xaml mvvm

我有一个 DataGrid,就 DataGrids 而言,它非常简单。出于某种原因,标题与其余数据不对齐,如下面的屏幕截图所示:

在此处输入图片说明

我已经在互联网上搜索过,但似乎无法找到解决方案。这是我的 DataGrid 代码:

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>
Run Code Online (Sandbox Code Playgroud)

还值得一提的是,当我单击一个 Combobox 单元格时,标题会正确对齐。

WPF*_*-it 3

您肯定有某种样式或隐藏了数据网格左上角 Select All按钮的东西。因此,列稍微向左移动。

使用此线程获取该按钮DataGrid.OnLoad并检查其Visibility属性。

全选按钮 WPF DataGrid

如果其折叠/隐藏,请将可见性设置为Visbility.Visible。或者检查其Width是否为零并设置适当的Width