如何使网格绘制空白单元格的边框?

4
我有一个ItemsControl,使用Grid作为ItemsPanelTemplate,并在ItemContainerStyle上设置Grid.Column和Grid.Row以定位网格中的数据项。
是否有一种方法可以向Grid添加GridLines,或者用Border填充空白单元格?
现在我已经将ShowGridLines设置为True,这使得虚线出现,但我想要水平线显示,并且我更喜欢实线的GridLines。
有相当大量的XAML,但以下是我的XAML布局截图:

你在布局方面使用了什么工具? - AdamBT
@AdamBT Balsamiz,他们有一个免费的网络演示,我通常使用它。 - Rachel
很好...非常感谢! - AdamBT
1个回答

5

很抱歉,无法轻松地对网格线进行样式设置。至少不是以一种简单的方式来实现。请参阅以下问题的解释:如何在WPF中更改Grid的网格线颜色?

MSDN文档表示:“仅提供点状线,因为此属性旨在作为设计工具来调试布局问题,而不是用于生产质量代码。如果您想在网格内部使用线条,请将网格内的元素设置为具有边框的样式。”

编辑: 如果您想要边框,可以创建自定义Grid并在控件的OnRender方法中绘制GridLines

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    namespace BorderGridControl
    {
        public class GridControl : Grid
        {
            #region Properties
            public bool ShowCustomGridLines
            {
                get { return (bool)GetValue(ShowCustomGridLinesProperty); }
                set { SetValue(ShowCustomGridLinesProperty, value); }
            }

            public static readonly DependencyProperty ShowCustomGridLinesProperty =
                DependencyProperty.Register("ShowCustomGridLines", typeof(bool), typeof(GridControl), new UIPropertyMetadata(false));

            
            public Brush GridLineBrush
            {
                get { return (Brush)GetValue(GridLineBrushProperty); }
                set { SetValue(GridLineBrushProperty, value); }
            }

            public static readonly DependencyProperty GridLineBrushProperty =
                DependencyProperty.Register("GridLineBrush", typeof(Brush), typeof(GridControl), new UIPropertyMetadata(Brushes.Black));

            public double GridLineThickness
            {
                get { return (double)GetValue(GridLineThicknessProperty); }
                set { SetValue(GridLineThicknessProperty, value); }
            }

            public static readonly DependencyProperty GridLineThicknessProperty =
                DependencyProperty.Register("GridLineThickness", typeof(double), typeof(GridControl), new UIPropertyMetadata(1.0));
            #endregion

            protected override void OnRender(DrawingContext dc)
            {
                if (ShowCustomGridLines)
                {
                    foreach (var rowDefinition in RowDefinitions)
                    {
                        dc.DrawLine(new Pen(GridLineBrush, GridLineThickness), new Point(0, rowDefinition.Offset), new Point(ActualWidth, rowDefinition.Offset));
                    }

                    foreach (var columnDefinition in ColumnDefinitions)
                    {
                        dc.DrawLine(new Pen(GridLineBrush, GridLineThickness), new Point(columnDefinition.Offset, 0), new Point(columnDefinition.Offset, ActualHeight));
                    }
                    dc.DrawRectangle(Brushes.Transparent, new Pen(GridLineBrush, GridLineThickness), new Rect(0, 0, ActualWidth, ActualHeight));
                }
                base.OnRender(dc);
            }
            static GridControl()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(GridControl), new FrameworkPropertyMetadata(typeof(GridControl)));
            }
        }
    }

我尝试过它,似乎运作得很好。
以下是使用示例:
    <controls:GridControl ShowCustomGridLines="True"
                          GridLineBrush="Red"
                          GridLineThickness="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </controls:GridControl>

是的,我看到了。我开始觉得我需要在Loaded事件中创建一个东西,用边框填充所有空白单元格。或者在彼此之上叠加两个网格,使顶部透明,底部填充边框。 - Rachel
你是根据项目在Xaml或代码中创建RowDefinitionsColumnDefinitions吗? - Fredrik Hedblad
在XAML中,每个“行”实际上都是一个具有7列的自己的“网格”。 - Rachel
@Rachel:请看我的更新答案,我相信这是迄今为止最简单的方法。 - Fredrik Hedblad
谢谢,看起来很有前途!我会在周一查看它(现在在家,有严格的禁止在家工作的政策)。 - Rachel
非常好,谢谢Meleak!我实际上将ShowCustomGridLines调整为了DataGridGridLinesVisibility类型,这样我就可以指定我想要水平线还是垂直线 :) - Rachel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接