Silverlight Grid网格线

来源:互联网 发布:重庆seo外包服务商 编辑:程序博客网 时间:2024/06/03 18:03

public class GridBorderHelper
        {
            ////A0B3C6
            public static SolidColorBrush _BorderBrush = new SolidColorBrush(ConvertFromString("#FFA0B3C6"));
            public static double _BorderThickness = 1;

            /// <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 == "autoBorder")
                        {

                            re = null;
                            gd.Children.RemoveAt(i);
                        }
                    }
                }

            }

            private static void CreateBorder(Grid gd, SolidColorBrush BorderBrush, double BorderThickness)
            {
                for (int i = 0; i < gd.RowDefinitions.Count; i++)
                {
                    for (int j = 0; j < gd.ColumnDefinitions.Count; j++)
                    {
                        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;
            }

        }

调用:GridBorderHelper.ShowBorder(gridInfo); 

gridinfo 为要显示网格线的Grid id

0 0
原创粉丝点击