在下面的窗口中,为什么标题的填充为2/3,即使我使用以下代码将其显式设置为0? (并且可以在Snoop中看到它为0):
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="0" />
</Style>
</DataGridTemplateColumn.HeaderStyle>
这给了我以下内容(标题有填充):
但是一旦我将其改为0以外的东西,例如:
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Padding" Value="0,1" />
<Setter Property="Margin" Value="0" />
</Style>
</DataGridTemplateColumn.HeaderStyle>
填充不会设置为默认值。我假设WPF库中有一些代码?
有人在遇到这个问题之前想出了为什么在我花了一个下午之前找出原因吗?
完整测试代码:
<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 :(得分:0)
您可以在VS2015中使用 Live ViusalTree 查看DataGridHeaderBorder
上方ContentPresenter
。
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;
}