本文介绍了仅一栏的特殊网格线样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 DataGrid ?特别是,我希望一列的左边界有一条双线.

How would I set a custom grid line style on just one column of a DataGrid? In particular, I'd like one column to have a double line as its left border.

示例:

| Col1 | Col2 || Col3 (w/ Double Left Border) |

谢谢你,

推荐答案

这取决于您希望在何处使用此双线.对于DataGridCell,垂直网格线绘制在OnRender中,对于DataGridCellsPresenter,水平网格线绘制在OnRender中.但是,DataGridColumnHeader的边界更加复杂.这是在DataGridHeaderBorderRenderTheme方法中绘制的矩形,我认为没有直接更改整个DataGridColumnHeader的宽度即可更改其宽度的直接方法.此外,标题的边框厚度是DataGrid中以"1px对2px"开头的单元格的厚度的两倍,这是因为标题在两侧都绘制了分隔符.

It depends on where you want this double line. The vertical GridLines is drawn in OnRender for DataGridCell and the horizontal GridLines is drawn in OnRender for DataGridCellsPresenter. The border for the DataGridColumnHeader is more complicated however. It's a Rectangle that's drawn in the RenderTheme method in DataGridHeaderBorder and I don't think that there is a direct way to change its width without re-templating the entire DataGridColumnHeader. Also, the border thickness for the Headers are twice as thick as the Cells in the DataGrid to begin with (1px vs 2px), because the Headers draw their Separators on both sides.

因此,要获得仅影响单元格的双线粗细,可以在希望应用的地方添加特殊的DataGridCell样式.该单元样式所做的全部工作就是向左绘制一个1px的边框,其颜色与GridLines相同.看起来像这样

So, to get double line thickness which just affects the Cells you can add a special DataGridCell style where you want this to apply. All this cellstyle does is to draw a 1px border to the left in the same colour as the GridLines. It'll look something like this

<DataGrid ...
          HorizontalGridLinesBrush="Black">
    <DataGrid.Resources>
        <Style x:Key="DoubleLeftBorderCell" TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="1,0,0,0"/>
            <Setter Property="BorderBrush" Value="{Binding ElementName=dataGrid, Path=HorizontalGridLinesBrush}"/>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Double left Border"
                            CellStyle="{StaticResource DoubleLeftBorderCell}"
                            Binding="{Binding TextProperty}"/>
    </DataGrid.Columns>
</DataGrid>

没有鼠标悬停效果或要担心的任何问题.但是,如果对DataGridColumnHeader做类似的操作,则会失去排序箭头,鼠标悬停效果,鼠标按下效果等.因此,您必须为其创建一个完整的模板

There is no mouseover effect or anything on the cells to worry about. If you do something similar for a DataGridColumnHeader however, you'll lose the sorting arrows, mouseover effect, mousedown effect etc. so a you'll have to create an entire template for it

这篇关于仅一栏的特殊网格线样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:40