如何让网格为空白单元格绘制边框?

Rac*_*hel 4 wpf grid layout xaml gridlines

我有一个 ItemsControl,它使用 aGrid作为ItemsPanelTemplate,并在 上设置 Grid.Column 和 Grid.Row 以ItemContainerStyle在网格中定位数据项

有没有办法将网格线添加到网格中,或者用边框填充空白单元格?

现在我已ShowGridLines设置为 True ,这会产生虚线,但是我希望显示水平线,并且我更喜欢实心网格线

截屏

有相当大量的 XAML,但下面是我的 XAML 布局的屏幕截图: XAML布局

Fre*_*lad 5

抱歉,无法设置网格线样式。至少不是以一种简单的方式。有关说明,请参阅以下问题:How can I change the color of the gridlines of a Grid in WPF?

MSDN 文档说“只有虚线可用,因为此属性旨在作为调试布局问题的设计工具,而不是用于生产质量代码。如果您想要网格内的线条,请将网格内的元素设置为具有边框”。

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

    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)));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试了一下,似乎效果很好。

这是一个使用示例:

    <controls:GridControl ShowCustomGridLines="True"
                          GridLineBrush="Red"
                          GridLineThickness="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </controls:GridControl>
Run Code Online (Sandbox Code Playgroud)