为WPF和Silverlight的Grid添加边框线

为WPF和Silverlight的Grid添加边框线(原文链接)


在此源代码的基础上,经过简单修改,保证了边框线条宽度统一.

修改后的源代码

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

namespace SilverlightApplicationGridBorderSample
{
    /// 
    /// 为Grid添加的一个特殊功能
    /// 作者:陈希章
    /// 反馈:[email protected]
    /// 
    public class GridHelper
    {

        //请注意:可以通过propa这个快捷方式生成下面三段代码

        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) =>
                {
                    //这种做法自动将控件移动到Border里面来
                    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);

                        Border border = new Border();
                        border.BorderBrush = new SolidColorBrush(Colors.LightGray);

                        //保证边框线条宽度统一
                        if ((0 == (int)row) && (0 == (int)column))
                        {
                            //第一行第一列
                            border.BorderThickness = new Thickness(1, 1, 1, 1);
                        }
                        else if (0 == (int)row)
                        {
                            //第一行
                            border.BorderThickness = new Thickness(0, 1, 1, 1);
                        }
                        else if (0 == (int)column)
                        {
                            //第一列
                            border.BorderThickness = new Thickness(1, 0, 1, 1);
                        }
                        else
                        {
                            //其它行列,即除去第一行,第一列以为的所有单元格
                            border.BorderThickness = new Thickness(0, 0, 1, 1);
                        }
                        border.Padding = new Thickness(2);
                        //{
                        //    BorderBrush = new SolidColorBrush(Colors.LightGray),
                        //    BorderThickness = new Thickness(1),
                        //    Padding = new Thickness(2)
                        //};
                        
                        Grid.SetRow(border, row);
                        Grid.SetColumn(border, column);
                        Grid.SetRowSpan(border, rowspan);
                        Grid.SetColumnSpan(border, columnspan);

                        grid.Children.RemoveAt(i);
                        border.Child = item;
                        grid.Children.Insert(i, border);
                    }
                };
            }
        }
    }
}



你可能感兴趣的:(C#,.NET)