您的位置:首页 > 其它

silverlight的grid显示边框和表格线

2012-12-23 11:25 387 查看
参考:http://blog.sina.com.cn/s/blog_591d774e0100x4vr.html。

原作者的显示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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: