silverlight的grid显示边框和表格线
2012-12-23 11:25
387 查看
参考:http://blog.sina.com.cn/s/blog_591d774e0100x4vr.html。
原作者的显示grid的时候没法对跨行和跨列进行特殊处理,在跨行跨列的情况下,显示的处理效果为:
其中红色部分为跨行或者跨列的单元格。
对原作者代码修改之后的效果为:
如上图所示,对跨行和跨列进行了特殊处理。
修改之后的源码为:
希望对大家有用,silverlight版本为4.0,感谢原作者:lighting_pig
原作者的显示grid的时候没法对跨行和跨列进行特殊处理,在跨行跨列的情况下,显示的处理效果为:
其中红色部分为跨行或者跨列的单元格。
对原作者代码修改之后的效果为:
如上图所示,对跨行和跨列进行了特殊处理。
修改之后的源码为:
public class GridBorderHelper { ////A0B3C6 public static SolidColorBrush _BorderBrush = new SolidColorBrush(ConvertFromString("#FFA0B3C6")); public static double _BorderThickness = 0.5; /// <summary> /// 生成边框 /// </summary> public static void ShowBorder(Grid gd) { if (gd == null) { return; } ClearBorder(gd); SolidColorBrush BorderColorBrush = _BorderBrush; double BorderThickness = _BorderThickness; CreateBorder(gd, BorderColorBrush, BorderThickness); } /// <summary> /// 生成边框 /// </summary> public static void ShowBorder(Grid gd, SolidColorBrush BorderColorBrush, double BorderThickness) { if (gd == null) { return; } ClearBorder(gd); CreateBorder(gd, BorderColorBrush, BorderThickness); } private static void ClearBorder(Grid gd) { for (int i = gd.Children.Count - 1; i >= 0; i--) { if (gd.Children[i].ToString() == "System.Windows.Controls.Border") { Border re = gd.Children[i] as Border; if (re.Tag.ToString() == "autoBorder") { gd.Children.RemoveAt(i); } } } } private static void CreateBorder(Grid gd, SolidColorBrush BorderBrush, double BorderThickness) { List<IgnoreGridLine> ignoreGridLine = new List<IgnoreGridLine>(); for (int i = gd.Children.Count - 1; i >= 0; i--) { UIElement ui = gd.Children[i]; int row = Convert.ToInt32(ui.GetValue(Grid.RowProperty)); int column = Convert.ToInt32(ui.GetValue(Grid.ColumnProperty)); int rowSpan = Convert.ToInt32(ui.GetValue(Grid.RowSpanProperty)); int columnSpan = Convert.ToInt32(ui.GetValue(Grid.ColumnSpanProperty)); //既跨行又跨列时 if (rowSpan > 1 && columnSpan > 1) { for (int k = 0; k < rowSpan; k++) { for (int l = 0; l < columnSpan; l++) { ignoreGridLine.Add(new IgnoreGridLine(row + k, column + l)); } } } //跨行时 if (rowSpan > 1 && columnSpan == 1) { for (int k = 0; k < rowSpan; k++) { ignoreGridLine.Add(new IgnoreGridLine(row + k, column)); } } //跨列时 if (rowSpan == 1 && columnSpan > 1) { for (int l = 0; l < columnSpan; l++) { ignoreGridLine.Add(new IgnoreGridLine(row, column + l)); } } //跨行或跨列时才会画线 if (rowSpan > 1 || columnSpan > 1) { Border boIn = new Border(); boIn.BorderBrush = BorderBrush; boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness); boIn.SetValue(Grid.RowProperty, row); boIn.SetValue(Grid.ColumnProperty, column); boIn.SetValue(Grid.RowSpanProperty, rowSpan); boIn.SetValue(Grid.ColumnSpanProperty, columnSpan); boIn.Tag = "autoBorder"; gd.Children.Add(boIn); } } //根据行列定义画线 for (int i = 0; i < gd.RowDefinitions.Count; i++) { for (int j = 0; j < gd.ColumnDefinitions.Count; j++) { IgnoreGridLine ignore = new IgnoreGridLine(i, j); if (!ignoreGridLine.Contains(ignore)) { Border boIn = new Border(); boIn.BorderBrush = BorderBrush; boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness); boIn.SetValue(Grid.ColumnProperty, j); boIn.SetValue(Grid.RowProperty, i); boIn.Tag = "autoBorder"; gd.Children.Add(boIn); } } } //画最外面的边框 Border bo = new Border(); bo.BorderBrush = BorderBrush; bo.BorderThickness = new Thickness(BorderThickness, BorderThickness, 0, 0); bo.SetValue(Grid.ColumnProperty, 0); bo.SetValue(Grid.RowProperty, 0); bo.SetValue(Grid.ColumnSpanProperty, gd.ColumnDefinitions.Count); bo.SetValue(Grid.RowSpanProperty, gd.RowDefinitions.Count); bo.Tag = "autoBorder"; gd.Children.Add(bo); } /// <summary> /// 把字符串转成颜色 #A0B3C6 /// </summary> /// <param name="colorString"></param> /// <returns></returns> public static Color ConvertFromString(string colorString) { Color color; try { Line line = (Line)XamlReader.Load("<Line xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Fill=\"" + colorString + "\" />"); color = (Color)line.Fill.GetValue(SolidColorBrush.ColorProperty); } catch { color = new Color(); } return color; } } /// <summary> /// 记录被忽略的行与列 /// </summary> internal struct IgnoreGridLine { public int Row; public int Column; public IgnoreGridLine(int row, int column) { Row = row; Column = column; } }
希望对大家有用,silverlight版本为4.0,感谢原作者:lighting_pig
相关文章推荐
- WPF Grid自定义行列时,边框的显示控制
- jqGrid与Struts2的结合应用(一) —— 显示基本Grid表格
- 定义Table时显示表格边框显示1px
- jqGrid与Struts2的结合应用(一) —— 显示基本Grid表格
- 表格中的数据为空时,td的边框不显示
- Gridpanel数据加载成功后,触发事件,让表格第一行的数据显示在下面的formpanel
- 解决ie6 表格td中无内容时边框不显示
- 解决表格没有内容不显示边框的代码
- QT Qwebview显示html 的table表格时,如果有td跨行td边框显示不出来的bug
- jqGrid与Struts2的结合应用(一) ------ 显示基本Grid表格 .
- 使用百度编辑器ueditor表格无法显示边框以及边框颜色等系列问题解决方案
- 为WPF和Silverlight的Grid添加边框线
- .net利用IOleCommandTarget接口实现Html编辑器表格边框的显示和隐藏(原创)
- css 解决表格边框不显示的问题
- 百度UEditor粘贴或插入的表格不显示边框的解决办法
- 关于ueditor表格不显示边框的两种形式
- JS实现控制表格只显示行边框或者只显示列边框的方法
- ie6表格边框不显示问题
- Extjs实现Grid表格显示【一】
- Extjs日期/可编辑表格grid/显示不正确————解决方案