WPF的DataGrid HeaderTemplate中神秘的填充 [英] WPF DataGrid HeaderTemplate Mysterious Padding

查看:1451
本文介绍了WPF的DataGrid HeaderTemplate中神秘的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放置在一个DataGrid的列标题中的图像一个按钮。电池模板也只是一个简单的按钮图像。

 <我:DataGridTemplateColumn>
    <我:DataGridTemplateColumn.HeaderTemplate>
        <的DataTemplate>
            <按钮工具提示=添加新模板NAME =AddNewTemplate点击=AddNewTemplate_Click>
                <图像源=../资源/ add.png/>
            < /按钮>
        < / DataTemplate中>
    < /我:DataGridTemplateColumn.HeaderTemplate>
    <我:DataGridTemplateColumn.CellTemplate>
        <的DataTemplate>
            <按钮工具提示=编辑模板NAME =EditTemplate点击=EditTemplate_Click标签={结合}>
                <图像源=../资源/ pencil.png/>
            < /按钮>
        < / DataTemplate中>
   < /我:DataGridTemplateColumn.CellTemplate>
< /我:DataGridTemplateColumn>
 

当呈现时,报头具有大约填充10-15px右侧只会导致细胞明显呈现在该宽度离去两面具有空闲空间的细胞按钮。作为一个像素完美主义者这惹恼了地狱了我。我最初认为这是对所显示的箭头空间排序的时候,但我已经整理双方的整个DataGrid和明确的列关闭。

下面是一个形象:

我想这是从什么是按钮的父元素填充。有谁知道一个方法来消除它?


请参阅:


解决方法:

 <我:DataGrid.Resources>
    <风格X:关键=和addHeader的TargetType ={X:类型中学项目:DataGridColumnHeader}>
        < setter属性=模板>
            < Setter.Value>
                <的ControlTemplate的TargetType ={X:类型中学项目:DataGridColumnHeader}>
                    <按钮工具提示=添加新模板NAME =AddNewTemplate点击=AddNewTemplate_Click保证金=4>
                        <图像源=../资源/ add.png/>
                    < /按钮>
                < /控件模板>
            < /Setter.Value>
        < /二传手>
    < /样式和GT;
< /我:DataGrid.Resources>
<我:DataGrid.Columns>
    <我:DataGridTemplateColumn HeaderStyle ={的StaticResource和addHeader}>
        <我:DataGridTemplateColumn.CellTemplate>
            <的DataTemplate>
                <按钮工具提示=编辑模板NAME =EditTemplate点击=EditTemplate_Click标签={结合}>
                    <图像源=../资源/ pencil.png/>
                < /按钮>
            < / DataTemplate中>
        < /我:DataGridTemplateColumn.CellTemplate>
    < /我:DataGridTemplateColumn>
< /我:DataGrid.Columns>
 

解决方案

史努比救援!

现在你更新我发现一个有趣的code在的 DataGridHeaderBorder.cs 的:

 保护覆盖面积ArrangeOverride(大小arrangeSize)
   {
     // ...
     如果(padding.Equals(新厚度()))
     {
       填充= DefaultPadding;
     }
     // ...
     child.Arrange(新的矩形(padding.Left,padding.Top,childWidth,childHeight));
   }
 

在哪里DefaultPadding不为0 ......即使它们不使用标准的填充他们安排孩子稍微移动。

如何解决这一问题?也许写自己的模板表头会做的伎俩。或者你可以尝试为图像切缘阴性。不喜欢没有这个方法。但我不能找到更好的东西呢。

I am placing a single button with an image in the header of a column of a DataGrid. The cell template is also just a simple button with an image.

<my:DataGridTemplateColumn>
    <my:DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <Button ToolTip="Add New Template" Name="AddNewTemplate" Click="AddNewTemplate_Click">
                <Image Source="../Resources/add.png"/>
            </Button>
        </DataTemplate>
    </my:DataGridTemplateColumn.HeaderTemplate>
    <my:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button ToolTip="Edit Template" Name="EditTemplate" Click="EditTemplate_Click" Tag="{Binding}">
                <Image Source="../Resources/pencil.png"/>
            </Button>
        </DataTemplate>
   </my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>

When rendered, the header has approximately 10-15px of padding on the right side only which causes the cells to obviously render at that width leaving the cell button having empty space on both sides. Being a pixel-perfectionist this annoys the hell out of me. I had initially thought that it was space for the arrows displayed when sorted but I have sorting disabled on both the entire DataGrid and explicitly on the column.

Here's a image:

I assume this is padding from whatever is the parent element of the button. Does anyone know a way to eliminate it?


See:


Solution:

<my:DataGrid.Resources>
    <Style x:Key="addHeader" TargetType="{x:Type myp:DataGridColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type myp:DataGridColumnHeader}">
                    <Button ToolTip="Add New Template" Name="AddNewTemplate" Click="AddNewTemplate_Click" Margin="4">
                        <Image Source="../Resources/add.png"/>
                    </Button>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</my:DataGrid.Resources>
<my:DataGrid.Columns>
    <my:DataGridTemplateColumn HeaderStyle="{StaticResource addHeader}">
        <my:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button ToolTip="Edit Template" Name="EditTemplate" Click="EditTemplate_Click" Tag="{Binding}">
                    <Image Source="../Resources/pencil.png"/>
                </Button>
            </DataTemplate>
        </my:DataGridTemplateColumn.CellTemplate>
    </my:DataGridTemplateColumn>
</my:DataGrid.Columns>

解决方案

Snoop to the rescue!

Now that you updated the question I found an interesting code in the DataGridHeaderBorder.cs:

   protected override Size ArrangeOverride(Size arrangeSize)
   {
     //...
     if (padding.Equals(new Thickness()))
     {
       padding = DefaultPadding;
     }
     //...
     child.Arrange(new Rect(padding.Left, padding.Top, childWidth, childHeight));
   }

Where DefaultPadding isn't 0... Even though they don't use standard Padding they arrange the child slightly moved.

How to fix this? Maybe writing your own template for the table header will do the trick. Or you could try negative margins for the image. Don't like neither of this approaches. But I can't find anything better yet.

这篇关于WPF的DataGrid HeaderTemplate中神秘的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆