silverlight的grid显示边框和表格线

参考:http://blog.sina.com.cn/s/blog_591d774e0100x4vr.html。

原作者的显示grid的时候没法对跨行和跨列进行特殊处理,在跨行跨列的情况下,显示的处理效果为:

其中红色部分为跨行或者跨列的单元格。

对原作者代码修改之后的效果为:

如上图所示,对跨行和跨列进行了特殊处理。

修改之后的源码为:

  1     public class GridBorderHelper
  2     {
  3         ////A0B3C6
  4         public static SolidColorBrush _BorderBrush = new SolidColorBrush(ConvertFromString("#FFA0B3C6"));
  5         public static double _BorderThickness = 0.5;
  6 
  7         /// <summary>
  8         /// 生成边框
  9         /// </summary>
 10         public static void ShowBorder(Grid gd)
 11         {
 12             if (gd == null)
 13             {
 14                 return;
 15             }
 16 
 17             ClearBorder(gd);
 18 
 19             SolidColorBrush BorderColorBrush = _BorderBrush;
 20             double BorderThickness = _BorderThickness;
 21 
 22             CreateBorder(gd, BorderColorBrush, BorderThickness);
 23 
 24         }
 25 
 26         /// <summary>
 27         /// 生成边框
 28         /// </summary>
 29         public static void ShowBorder(Grid gd, SolidColorBrush BorderColorBrush, double BorderThickness)
 30         {
 31             if (gd == null)
 32             {
 33                 return;
 34             }
 35 
 36             ClearBorder(gd);
 37 
 38             CreateBorder(gd, BorderColorBrush, BorderThickness);
 39 
 40         }
 41 
 42         private static void ClearBorder(Grid gd)
 43         {
 44             for (int i = gd.Children.Count - 1; i >= 0; i--)
 45             {
 46                 if (gd.Children[i].ToString() == "System.Windows.Controls.Border")
 47                 {
 48                     Border re = gd.Children[i] as Border;
 49                     if (re.Tag.ToString() == "autoBorder")
 50                     {
 51                         gd.Children.RemoveAt(i);
 52                     }
 53                 }
 54             }
 55 
 56         }
 57 
 58         private static void CreateBorder(Grid gd, SolidColorBrush BorderBrush, double BorderThickness)
 59         {
 60             List<IgnoreGridLine> ignoreGridLine = new List<IgnoreGridLine>();
 61 
 62             for (int i = gd.Children.Count - 1; i >= 0; i--)
 63             {
 64                 UIElement ui = gd.Children[i];
 65                 int row = Convert.ToInt32(ui.GetValue(Grid.RowProperty));
 66                 int column = Convert.ToInt32(ui.GetValue(Grid.ColumnProperty));
 67                 int rowSpan = Convert.ToInt32(ui.GetValue(Grid.RowSpanProperty));
 68                 int columnSpan = Convert.ToInt32(ui.GetValue(Grid.ColumnSpanProperty));
 69 
 70                 //既跨行又跨列时
 71                 if (rowSpan > 1 && columnSpan > 1)
 72                 {
 73                     for (int k = 0; k < rowSpan; k++)
 74                     {
 75                         for (int l = 0; l < columnSpan; l++)
 76                         {
 77                             ignoreGridLine.Add(new IgnoreGridLine(row + k, column + l));
 78                         }
 79                     }
 80                 }
 81 
 82                 //跨行时
 83                 if (rowSpan > 1 && columnSpan == 1)
 84                 {
 85                     for (int k = 0; k < rowSpan; k++)
 86                     {
 87 
 88                         ignoreGridLine.Add(new IgnoreGridLine(row + k, column));
 89 
 90                     }
 91                 }
 92 
 93                 //跨列时
 94                 if (rowSpan == 1 && columnSpan > 1)
 95                 {
 96                     for (int l = 0; l < columnSpan; l++)
 97                     {
 98                         ignoreGridLine.Add(new IgnoreGridLine(row, column + l));
 99                     }
100                 }
101 
102                 //跨行或跨列时才会画线
103                 if (rowSpan > 1 || columnSpan > 1)
104                 {
105                     Border boIn = new Border();
106 
107                     boIn.BorderBrush = BorderBrush;
108                     boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness);
109                     boIn.SetValue(Grid.RowProperty, row);
110                     boIn.SetValue(Grid.ColumnProperty, column);
111                     boIn.SetValue(Grid.RowSpanProperty, rowSpan);
112                     boIn.SetValue(Grid.ColumnSpanProperty, columnSpan);
113                     boIn.Tag = "autoBorder";
114                     gd.Children.Add(boIn);
115                 }
116             }
117 
118             //根据行列定义画线
119             for (int i = 0; i < gd.RowDefinitions.Count; i++)
120             {
121                 for (int j = 0; j < gd.ColumnDefinitions.Count; j++)
122                 {
123                     IgnoreGridLine ignore = new IgnoreGridLine(i, j);
124                     if (!ignoreGridLine.Contains(ignore))
125                     {
126                         Border boIn = new Border();
127 
128                         boIn.BorderBrush = BorderBrush;
129                         boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness);
130                         boIn.SetValue(Grid.ColumnProperty, j);
131                         boIn.SetValue(Grid.RowProperty, i);
132                         boIn.Tag = "autoBorder";
133                         gd.Children.Add(boIn);
134                     }
135                 }
136             }
137 
138             //画最外面的边框
139             Border bo = new Border();
140             bo.BorderBrush = BorderBrush;
141             bo.BorderThickness = new Thickness(BorderThickness, BorderThickness, 0, 0);
142             bo.SetValue(Grid.ColumnProperty, 0);
143             bo.SetValue(Grid.RowProperty, 0);
144 
145             bo.SetValue(Grid.ColumnSpanProperty, gd.ColumnDefinitions.Count);
146             bo.SetValue(Grid.RowSpanProperty, gd.RowDefinitions.Count);
147 
148             bo.Tag = "autoBorder";
149             gd.Children.Add(bo);
150         }
151 
152 
153         /// <summary>
154         /// 把字符串转成颜色 #A0B3C6
155         /// </summary>
156         /// <param name="colorString"></param>
157         /// <returns></returns>
158         public static Color ConvertFromString(string colorString)
159         {
160             Color color;
161             try
162             {
163                 Line line = (Line)XamlReader.Load("<Line xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Fill=\"" + colorString + "\" />");
164                 color = (Color)line.Fill.GetValue(SolidColorBrush.ColorProperty);
165             }
166             catch
167             {
168                 color = new Color();
169             }
170 
171             return color;
172         }
173 
174     }
175 
176     /// <summary>
177     /// 记录被忽略的行与列
178     /// </summary>
179     internal struct IgnoreGridLine
180     {
181         public int Row;
182         public int Column;
183 
184         public IgnoreGridLine(int row, int column)
185         {
186             Row = row;
187             Column = column;
188         }
189     }

希望对大家有用,silverlight版本为4.0,感谢原作者:lighting_pig

转载于:https://www.cnblogs.com/wangjianpub/archive/2012/12/23/2829792.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值