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

2024-02-21

我有一个 ItemsControl 它使用Grid as the ItemsPanelTemplate,并设置 Grid.Column 和 Grid.RowItemContainerStyle在网格中定位数据项

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

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

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


抱歉,无法设置网格线样式。至少不是以一种简单的方式。请参阅以下问题以获取解释:如何更改 WPF 中 Grid 的网格线颜色? https://stackoverflow.com/questions/606220/how-can-i-change-the-color-of-the-gridlines-of-a-grid-in-wpf

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

Edit:如果您想要边框,可以创建自定义边框Grid并画出GridLines in 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)));
            }
        }
    }

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

这是一个使用示例:

    <controls:GridControl ShowCustomGridLines="True"
                          GridLineBrush="Red"
                          GridLineThickness="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </controls:GridControl>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让网格为空白单元格绘制边框? 的相关文章

随机推荐

  • 没有默认构造函数?

    include
  • 如何防止 Visual Studio 在 Web 项目中“发布”XML 文档文件?

    Note 这个问题类似于如何防止在发布模式构建中复制 XML 文档文件 https stackoverflow com questions 3980958 how to prevent the copy of xml documentati
  • 对分组表进行数学运算

    我的问题不在于真正的编程语言 我有一个 ABAP 语言练习 但该语言不是很重要 无论如何 我有一张桌子 我需要计算该职位的总成本 显然是在选择之后 然后 该表将按两个字段 MATNR 和 BUKRS 进行分组 因此我需要知道每个组的头寸总成
  • Dart JsonSerialized 与抽象类

    我正在尝试为一个对象生成 json 辅助函数 该函数包含一个具有抽象类类型的列表 如下所示 import package json annotation json annotation dart import exercise variat
  • 数据库设计 - 两个项目应该共享同一个表吗?

    背景 同时设计的两个项目 A B 都需要一个新表 称为DocumentStore 在 postgres 下存储文档 文件 但是项目 A 和 B 之间围绕文档存储的业务逻辑是不同的 这意味着围绕文档存储的关系DocumentStoreA 和
  • java.lang.RuntimeException 找不到 FacesContext

    我不知道如何继续 但我的新 JSF 1 2 Web 应用程序总是收到 java lang RuntimeException 找不到 FacesContext 我确信这只是我找不到的一些配置 异常发生在第一个f or h 标签 已经有了重要的
  • korn 和 bash shell 之间的区别[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我对 Unix 完全陌生 目前 我被要求了解这两个方面康壳 ksh and bash shell 有人可以给我简要介绍一下这两者吗 是这个
  • Camera2cameraManager.openCamera在某些设备上有时会出现异常

    我有一个录制视频的应用程序 目前安装量约为 80K 超过 100K 下载量 Crashlytics 统计数据似乎不错 无崩溃用户 99 66 Google Play 上的评分也不错 4 5 但对于我的一些用户来说 仍然可能会发生下一个错误
  • 如何让 SwiftUI 的 .onContinueUserActivity 工作

    我很好奇 有没有人设法让 SwiftUI 的 onContinueUserActivity 工作 看着来自苹果的示例代码 https developer apple com documentation swiftui restoring y
  • 实体对象不能被 IEntityChangeTracker 的多个实例引用

    我使用 EF 作为 ORM 我处置objectContext根据每一个要求 我将实体保存在缓存层中 因为我的服务获得大量流量 我有时会收到错误objectContext already disposed对于我从缓存中获取的一些实体 我已将此
  • ## 预处理器运算符有哪些应用以及需要考虑的问题?

    正如我之前的许多问题中提到的 我正在使用 K R 目前正在使用预处理器 更有趣的事情之一是我之前学习 C 的尝试中从未了解过的事情 预处理器运算符 根据 K R 的说法 预处理器运算符 提供了一种连接实际的方法 宏展开期间的参数 如果一个
  • 组件返回失败代码:0x80004005

    仅当我执行此操作时 有时才会触发此错误XMLHttpRequest uncaught exception Exception Component returned failure code 0x80004005 NS ERROR FAILU
  • Ruby - 是否可以将方法别名为安全导航运算符

    Ruby 2 3 引入了安全导航运算符 但我发现它的语法过于离散 在简单扫描代码时很容易错过 相反 我更喜欢以下语法try因为它看起来更加明显和有意 所以我的问题是在 Ruby 2 3 中 有没有办法为安全导航运算符添加别名或猴子修补方法
  • 从Python调用Matlab函数

    我有一个项目 其中有一个一个 matlab 代码 我必须运行 Django 我尝试安装 Mlabwrap 但它给了我以下错误 Traceback most recent call last File
  • 应用程序不再可用

    在移动设备上安装应用程序后 并将设备时间更改为一周前 并尝试打开应用程序它说 MyAppName Is No Longer Available 任何遇到此问题的人请告诉我 您的意见非常重要 我们有很多理由这样做 如果您没有有效的开发者许可证
  • 使用 tweepy 保存推文全文

    我是一个Python新手程序员 我在尝试使用以下命令提取一系列推文的文本时遇到问题tweepy并将其保存到文本文件 我省略了身份验证和其他内容 search api search hello count 10 textlist for i
  • 解析本地HTML文件

    我可以使用 PowerShell 解析 HTML 页面 PS gt foo Invoke WebRequest http example com PS gt foo Links Count 1 但是 如果我下载该页面 PS gt Invok
  • 结构错误中的联合

    我有以下结构 struct type1 struct type2 node union element struct type3 e int val 初始化指针时 f指向一个实例type1并做类似的事情 f element gt e甚至只是
  • 在 MATLAB 中更改 seqlogo 图形的 x 轴

    我正在制作大量seqlogos http www mathworks com access helpdesk help toolbox bioinfo ref seqlogo html以编程方式 它们有数百列宽 因此运行seqlogo通常会
  • 如何让网格为空白单元格绘制边框?

    我有一个 ItemsControl 它使用Grid as the ItemsPanelTemplate 并设置 Grid Column 和 Grid RowItemContainerStyle在网格中定位数据项 有没有办法将网格线添加到网格