如何让网格为空白单元格绘制边框? [英] How can I get a Grid to draw Borders for blank cells?

查看:17
本文介绍了如何让网格为空白单元格绘制边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ItemsControl which uses a Grid as the ItemsPanelTemplate, and sets the Grid.Column and Grid.Row on the ItemContainerStyle to position data items in the grid

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

Is there a way to either add GridLines to the Grid, or to fill in the blank cells with a Border?

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

Right now I have ShowGridLines set to True which makes the dotted lines, however I want Horizontal lines to show up, and I'd prefer solid GridLines

有相当多的 XAML,但这里是我的 XAML 布局的屏幕截图:

There is a rather large amount of XAML, but here's a screenshot of how my XAML is laid out:

推荐答案

抱歉,无法设置网格线的样式.至少不是以简单的方式.请参阅以下问题以获取解释:如何在 WPF 中更改 Grid 的网格线颜色?

Sorry, styling the grid lines can't be done. Atleast not in an easy way. See the following question for an explanation: How can I change the color of the gridlines of a Grid in WPF?

MSDN 文档说只有虚线可用,因为此属性旨在作为调试布局问题的设计工具,而不是用于生产质量代码.如果你想要里面的线条一个 Grid,将 Grid 中的元素设置为有边框."

MSDN docs say "Only dotted lines are available because this property is intended as a design tool to debug layout problems and is not intended for use in production quality code. If you want lines inside a Grid, style the elements within the Grid to have borders."

如果你想要边框,你可以创建一个自定义 Grid 并在 OnRender 中绘制 GridLines控制方法.

If you want the borders you can create a custom Grid and draw the GridLines in OnRender method of the control.

    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)));
            }
        }
    }

我试过了,它似乎工作得很好.

I Tried it out and it seems to be working great.

这里是一个使用示例:

    <controls:GridControl ShowCustomGridLines="True"
                          GridLineBrush="Red"
                          GridLineThickness="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </controls:GridControl>

这篇关于如何让网格为空白单元格绘制边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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