2012-10-30 45 views
4

我有一个DataGrid,就DataGrids而言,这非常简单。出于某种原因或其他,头不与其他数据对齐,如图下面的截图:DataGrid列标题不与数据对齐

enter image description here

我在网上搜索,但似乎无法找到它的解决方案。这里是我的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> 

这也可能是值得一提的是,当我点击组合框的细胞之一,头赞同正常。

+0

@Blam你是什么意思? –

回答

3

你肯定有一些风格或东西是隐藏左上方的DataGrid的Select All按钮。因此,列向左移动一点。

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

Select All button WPF DataGrid

如果其collpased /隐藏,能见度设置为Visbility.Visible。或者检查它的Width为零并设置合适的Width

+0

只需将宽度设置为5,并做了诀窍。谢谢:) –

0

您可以使用DataGrid一样的性能,还可以定义特定CSS类的行

HeaderStyle-HorizontalAlign="" 
HeaderStyle-CssClass="" 
+0

我相信这不会为WPF应用程序工作^^ –

+0

我的问题是与WPF DataGrid,虽然,而不是ASP.NET –

1

我发现,简单地设置HeadersVisiblity“列”的伎俩 - 见下文XAML。
比更容易获得该SelectAll Button的保持......

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

使用这个技巧不能解决问题,它只是隐藏行选择按钮。而不是缺少一个按钮(全选),现在您将拥有两个按钮。 – Octan

2

最后,我打败了这个问题 查找the solution here

对不起,我没有注意到你没有重写DataGrid控件模板。恐怕,您将不得不定义一个控件模板以纠正DataGrid行为。

PS:我有.NET Framework 4.0