添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
威武的冰棍  ·  iOS 二进制流转化_ios ...·  2 周前    · 
追风的手链  ·  OpenStack Keystone ...·  1 年前    · 
八块腹肌的生菜  ·  go - ...·  1 年前    · 
本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《 阿里云开发者社区用户服务协议 》和 《 阿里云开发者社区知识产权保护指引 》。如果您发现本社区中有涉嫌抄袭的内容,填写 侵权投诉表单 进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。 相信大家在做WPF项目的时候,都会用到Grid这个布局控件,一般情况下,如果只是为了布局,那就不需要显示它的边框,但是也有特殊需求,如果把它当做表格来使用,那就需要为它添加实线边框。下面帖代码:

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

namespace WPFStart
为Grid添加的一个特殊功能,实线边框
public class GridHelper
//边框的宽度
static double myBorderWidth = 1;

public static double GetBorderWidth(DependencyObject obj)
return (double)obj.GetValue(BorderWidthProperty);
public static void SetBorderWidth(DependencyObject obj, double value)
obj.SetValue(BorderWidthProperty, value);
public static readonly DependencyProperty BorderWidthProperty DependencyProperty.RegisterAttached("BorderWidth", typeof(double), typeof(GridHelper), new PropertyMetadata(OnBorderWidthChanged));

private static void OnBorderWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
myBorderWidth = (double)e.NewValue;
public static bool GetShowBorder(DependencyObject obj)
return (bool)obj.GetValue(ShowBorderProperty);
public static void SetShowBorder(DependencyObject obj, bool value)
obj.SetValue(ShowBorderProperty, value);
public static readonly DependencyProperty ShowBorderProperty DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(GridHelper), new PropertyMetadata(OnShowBorderChanged));

//这是一个事件处理程序,需要手工编写,必须是静态方法
private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
var grid = d as Grid;
if ((bool)e.OldValue)
grid.Loaded -= (s, arg) => { };
if ((bool)e.NewValue)
grid.Loaded += (s, arg) =>
//根据Grid的顶层子控件的个数去添加边框,同时考虑合并的情况
var controls = grid.Children;
var count = controls.Count;
for (int i = 0; i < count; i++)
var item = controls[i] as FrameworkElement;
var row = Grid.GetRow(item);
var column = Grid.GetColumn(item);
var rowspan = Grid.GetRowSpan(item);
var columnspan = Grid.GetColumnSpan(item);

//设置边框线的颜色
var border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.White);

if (row == grid.RowDefinitions.Count - 1 && column == grid.ColumnDefinitions.Count - 1)
border.BorderThickness = new Thickness(myBorderWidth);
else if (row == grid.RowDefinitions.Count - 1)
border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, 0, 0);
else if (column == grid.ColumnDefinitions.Count - 1)
border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, myBorderWidth, 0);
border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth,0, 0 );

Grid.SetRow(border, row);
Grid.SetColumn(border, column);
Grid.SetRowSpan(border, rowspan);
Grid.SetColumnSpan(border, columnspan);
grid.Children.Add(border);

在项目中新建这个类,在XAML页面引用设置属性时,需要添加 “xmlns:ext="clr-namespace:WPFGrid(项目名称)"“,“ ext:GridHelper.ShowBorder="true"”这里就是设置边框是否显示了。

******WPF XAML技术交流群:477319939******

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangli321456/article/details/80064509