WPF利用附加属性修改ShowGridLines效果
1.思路主要代码
wpf的gridline原本效果是虚线类型的。有时候需要设计成表格形式的,因此有了用附加属性来自动绘制边框线的想法。
思路:绘制Line并添加到grid的children里,但效果并不理想,会出现锯齿,像素对齐,模糊等问题。
UseLayoutRounding="False"
SnapsToDevicePixels="True"
RenderOptions.EdgeModeProperty 貌似都没起作用。
于是想到了用border来实现,简单又实用吧 哈哈。
大致思路如下:绘制border的左边框和上边框,在边界的时候考虑边界封闭。然后将border平移一半的距离。这样边框就居中并且包围了所有的线。

主要代码如下:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media; namespace 用附加属性修改Grid的边框
{
public class GridHelper
{
private static void RefreshGrid(Grid grid, int lineWidth, Brush color)
{
for (var i = grid.Children.Count - 1; i > 0; i--)
{
var child = grid.Children[i]; var bd = child as Border;
if (bd != null && bd.Tag != null && bd.Tag.ToString() == "gridline")
{
grid.Children.Remove(bd);
}
}
var rows = grid.RowDefinitions.Count;
var cols = grid.ColumnDefinitions.Count;
//边界考虑
if (rows == 0)
{
rows = 1;
}
if (cols == 0)
{
cols = 1;
}
//生成行列
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < cols; j++)
{
var thick = new Thickness(lineWidth, lineWidth, 0, 0);
var margin = new Thickness(-lineWidth/2d, -lineWidth/2d, 0, 0);
//边界考虑
if (i == 0)
{
margin.Top = 0;
}
if (i == rows - 1)
{
thick.Bottom = lineWidth;
}
if (j == 0)
{
margin.Left = 0;
}
if (j == cols - 1)
{
thick.Right = lineWidth;
}
var bd = new Border
{
BorderThickness = thick,
Margin = margin,
BorderBrush = color,
Tag = "gridline"
};
Grid.SetRow(bd, i);
Grid.SetColumn(bd, j);
grid.Children.Add(bd);
}
}
grid.InvalidateArrange();
grid.InvalidateVisual();
} #region 线颜色 // Using a DependencyProperty as the backing store for LineColor. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LineColorProperty =
DependencyProperty.RegisterAttached("LineColor", typeof (Brush), typeof (GridHelper),
new PropertyMetadata(Brushes.Black, LineColorPropertyChanged)); public static Brush GetLineColor(DependencyObject obj)
{
return (Brush) obj.GetValue(LineColorProperty);
} public static void SetLineColor(DependencyObject obj, Brush value)
{
obj.SetValue(LineColorProperty, value);
} private static void LineColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = d as Grid;
if (grid == null)
{
return;
}
var showLines = GetShowGridLines(grid);
var color = GetLineColor(grid);
var lineWidth = GetLineWidth(grid);
if (showLines)
{
// grid.SnapsToDevicePixels = true;
grid.Loaded += delegate { RefreshGrid(grid, lineWidth, color); };
}
} #endregion #region 线宽度 // Using a DependencyProperty as the backing store for LineWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LineWidthProperty =
DependencyProperty.RegisterAttached("LineWidth", typeof (int), typeof (GridHelper),
new PropertyMetadata(1, LineWidthPropertyChanged)); public static int GetLineWidth(DependencyObject obj)
{
return (int) obj.GetValue(LineWidthProperty)
;
} public static void SetLineWidth(DependencyObject obj, int value)
{
obj.SetValue(LineWidthProperty, value);
} private static void LineWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = d as Grid;
if (grid == null)
{
return;
}
var showLines = GetShowGridLines(grid);
var color = GetLineColor(grid);
var lineWidth = GetLineWidth(grid);
if (showLines)
{
// grid.SnapsToDevicePixels = true;
grid.Loaded += delegate { RefreshGrid(grid, lineWidth, color); };
}
} #endregion #region 是否显示线 // Using a DependencyProperty as the backing store for ShowGridLines. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowGridLinesProperty =
DependencyProperty.RegisterAttached("ShowGridLines", typeof (bool), typeof (GridHelper),
new PropertyMetadata(false, ShowGridLinesPropertyChanged)); public static bool GetShowGridLines(DependencyObject obj)
{
return (bool) obj.GetValue(ShowGridLinesProperty);
} public static void SetShowGridLines(DependencyObject obj, bool value)
{
obj.SetValue(ShowGridLinesProperty, value);
} private static void ShowGridLinesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grid = d as Grid;
if (grid == null)
{
return;
}
var showLines = GetShowGridLines(grid);
var color = GetLineColor(grid);
var lineWidth = GetLineWidth(grid);
if (showLines)
{
// grid.SnapsToDevicePixels = true;
grid.Loaded += delegate { RefreshGrid(grid, lineWidth, color); };
}
} #endregion
}
}
2.效果图
效果还可以,任何分辨率下,任何边框大小,都没有出现像素对齐或者模糊问题。 图中的虚线是grid的默认gridLine,红色和绿色是自定义的gridline,跟虚线完美重合。

3.源码下载
https://files.cnblogs.com/files/chlm/%E7%94%A8%E9%99%84%E5%8A%A0%E5%B1%9E%E6%80%A7%E4%BF%AE%E6%94%B9Grid%E7%9A%84%E8%BE%B9%E6%A1%86.rar
WPF利用附加属性修改ShowGridLines效果的更多相关文章
- WPF中利用RadialGradient模拟放大镜效果
原文:WPF中利用RadialGradient模拟放大镜效果 --------------------------------------------------------------------- ...
- WPF Adorner+附加属性 实现控件友好提示
标题太空泛,直接上图 无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成. 例如WPF自带的控件上要加这样的效 ...
- 利用Photoshop修改图片以达到投稿要求
摘自:http://www.dxy.cn/bbs/thread/8602152#8602152 利用Photoshop修改图片以达到投稿要求 软件版本为Photoshop CS V8.0.1(中文版) ...
- WPF利用动画实现圆形进度条
原文:WPF利用动画实现圆形进度条 这是我的第一篇随笔,最近因为工作需要,开始学习WPF相关技术,自己想实现以下圆形进度条的效果,逛了园子发现基本都是很久以前的文章,实现方式一般都是GDI实现的,想到 ...
- WPF 图片浏览 伪3D效果
原文:WPF 图片浏览 伪3D效果 首先上效果图: 因项目要求,需要把图片以"好看"."炫"的效果展示出来,特地研究了一下WPF关于3D方面的制作,奈何最终成果 ...
- 【WPF】两则动画效果
原文:[WPF]两则动画效果 引言 利用WPF的动画可以轻而易举的实现各种各样的特效,如擦除,滑动进入等,先看两个效果图 第一个效果 这个动画其实利用了OpacityMask和LinearGradie ...
- WPF利用HelixToolKit后台导入3D模型
原文:WPF利用HelixToolKit后台导入3D模型 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/de ...
- 用WPF轻松打造iTunes CoverFlow效果
原文:用WPF轻松打造iTunes CoverFlow效果 用WPF轻松打造iTunes CoverFlow效果 ...
- c# wpf 利用截屏键实现截屏功能
原文:c# wpf 利用截屏键实现截屏功能 最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下. 先是找到了这样一份代码: static class Scr ...
随机推荐
- 利用sfc文件构建网络渗透
收集哈希 SCF(Shell命令文件)文件可用于执行一组有限的操作,例如显示Windows桌面或打开Windows资源管理器,这并不是什么新鲜事.然而,一个SCF文件可以用来访问一个特定的UNC路 ...
- 仿vue实现简易版mvvm双向绑定
项目地址:https://github.com/pangyongsheng/mvvm 1. 指令 vm-bind 单选数据绑定- 将数据显示到标签视图 vm-model : 双向数据绑定 vm-sho ...
- poj supermaket (贪心)
http://poj.org/problem?id=1456 #include<cstring> #include<iostream> #include<algorith ...
- STL --> vector向量
vector向量 vector是一种对象实体,能够容纳许多其他类型相同的元素,因为又被称为容器. 头文件 在使用它时,需要包含头文件 <vector>. #include <vect ...
- [poj3280]Cheapest Palindrome_区间dp
Cheapest Palindrome poj-3280 题目大意:给出一个字符串,以及每种字符的加入代价和删除代价,求将这个字符串通过删减元素变成回文字符串的最小代价. 注释:每种字符都是小写英文字 ...
- UI线程异常处理方法
当应用程序启动,创建了一个叫“main”的线程,用于管理UI相关,又叫UI线程.其他线程叫工作线程(Work Thread). Single Thread Model 一个组件的创建并不会新建一个线程 ...
- C第九次博客作业--指针
一.PTA实验作业 题目1:两个4位正整数的后两位互换 1. 本题PTA提交列 2. 设计思路 3.代码截图 本题调试过程碰到问题及PTA提交列表情况说明 刚开始想到的交换是令t=a;a=b;b=t这 ...
- 设计模式NO.3
设计模式NO.3 本次博客内容为第三次设计模式的练习.根据老师的要求完成下列题目: 题目1 某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商 ...
- python3变量和数据类型
变量和数据类型 知识点 python 关键字 变量的定义与赋值 input() 函数 字符串的格式化 实验步骤 每一种编程语言都有它们自己的语法规则,就像我们所说的外语. 1. 关键字和标识符 ...
- 201421123042 《Java程序设计》第2周学习总结
1. 本周学习总结 以几个关键词描述本周的学习内容.并将关键词之间的联系描述或绘制出来. 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴. 引用类型 引用类型是指向一个对象,感觉 ...