2016-08-23 78 views
0

在下面的窗口中,爲什麼頁眉的填充爲2/3,即使我使用下面的代碼明確地將其設置爲0? (而且可以把它看成0探聽):WPF DataGrid奇怪的頁眉填充行爲

<DataGridTemplateColumn.HeaderStyle> 
    <Style TargetType="DataGridColumnHeader"> 
     <Setter Property="Padding" Value="0" /> 
     <Setter Property="Margin" Value="0" /> 
    </Style> 
</DataGridTemplateColumn.HeaderStyle> 

這給了我下面的(頭有邊距):

enter image description here

但只要我將其更改爲0以外的東西,例如:

<DataGridTemplateColumn.HeaderStyle> 
    <Style TargetType="DataGridColumnHeader"> 
     <Setter Property="Padding" Value="0,1" /> 
     <Setter Property="Margin" Value="0" /> 
    </Style> 
</DataGridTemplateColumn.HeaderStyle> 

填充未設置爲默認值。我假設有一些代碼在WPF庫中的某處?

enter image description here

已經有人穿過這之前來想通了,爲什麼我花一個下午查明原因之前?

全面測試代碼:

<Window x:Class="WpfApplication1.GridSpacing" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:system="clr-namespace:System;assembly=mscorlib" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <DataGrid> 
      <DataGrid.Items> 
       <system:String>Test</system:String> 
      </DataGrid.Items> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.HeaderTemplate> 
         <DataTemplate> 
          <TextBlock Text="Test" 
             Background="Aquamarine" 
             HorizontalAlignment="Stretch" 
             VerticalAlignment="Stretch"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.HeaderTemplate> 
        <DataGridTemplateColumn.HeaderStyle> 
         <Style TargetType="DataGridColumnHeader"> 
          <Setter Property="Padding" Value="0" /> 
         </Style> 
        </DataGridTemplateColumn.HeaderStyle> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
           <TextBlock Text="Test" Background="Aquamarine"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

回答

0

你可以看到有DataGridHeaderBorder使用直播ViusalTree在VS2015的ContentPresenter以上。

enter image description here

DataGridHeaderBorder的問題。你可以參考這個answer。在DataGridHeaderBorder.cs

/// <summary> 
///  Positions children and returns the final size of the element. 
/// </summary> 
protected override Size ArrangeOverride(Size arrangeSize) 
{ 
    if (UsingBorderImplementation) 
    { 
     // Revert to the Border implementation 
     return base.ArrangeOverride(arrangeSize); 
    } 

    UIElement child = Child; 
    if (child != null) 
    { 
     // Use the public Padding property if it's set 
     Thickness padding = Padding; 
     if (padding.Equals(new Thickness())) 
     { 
      padding = DefaultPadding; 
     } 

     // Reserve space for the chrome 
     double childWidth = Math.Max(0.0, arrangeSize.Width - padding.Left - padding.Right); 
     double childHeight = Math.Max(0.0, arrangeSize.Height - padding.Top - padding.Bottom); 

     child.Arrange(new Rect(padding.Left, padding.Top, childWidth, childHeight)); 
    } 

    return arrangeSize; 
}