WPF Grid边框

WPF Grid布局自带的属性没有边框

1、头部引入​

xmlns:ext="clr-namespace:TS.HY.Manage"

2​、Grid设置属性

 

                ext:GridHelper.ShowBorder="True"

                ext:GridHelper.GridLineThickness="1"

                ext:GridHelper.GridLineBrush="black">

​3、GridHelper源码

public class GridHelper

    {

        public static readonly DependencyProperty ShowBorderProperty =

            DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(GridHelper),

        new PropertyMetadata(OnShowBorderChanged));

        public static readonly DependencyProperty GridLineThicknessProperty =

            DependencyProperty.RegisterAttached("GridLineThickness", typeof(double), typeof(GridHelper),

            new PropertyMetadata(OnGridLineThicknessChanged));

        public static readonly DependencyProperty GridLineBrushProperty =

            DependencyProperty.RegisterAttached("GridLineBrush", typeof(Brush), typeof(GridHelper),

            new PropertyMetadata(OnGridLineBrushChanged));

        #region ShowBorder

        public static bool GetShowBorder(DependencyObject obj)

        {

            return (bool)obj.GetValue(ShowBorderProperty);

        }

        public static void SetShowBorder(DependencyObject obj, bool value)

        {

            obj.SetValue(ShowBorderProperty, value);

        }

        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 += new RoutedEventHandler(GridLoaded);

            }

        }

        #endregion

        #region GridLineThickness

        public static double GetGridLineThickness(DependencyObject obj)

        {

            return (double)obj.GetValue(GridLineThicknessProperty);

        }

        public static void SetGridLineThickness(DependencyObject obj, double value)

        {

            obj.SetValue(GridLineThicknessProperty, value);

        }

        private static void OnGridLineThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

        }

        #endregion

        #region GridLineBrush

        public static Brush GetGridLineBrush(DependencyObject obj)

        {

            Brush brush = (Brush)obj.GetValue(GridLineBrushProperty);

            return brush == null ? Brushes.LightGray : brush;

        }

        public static void SetGridLineBrush(DependencyObject obj, Brush value)

        {

            obj.SetValue(GridLineBrushProperty, value);

        }

        private static void OnGridLineBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

        }

        #endregion

        private static void GridLoaded(object sender, RoutedEventArgs e)

        {

            Grid grid = sender as Grid;

            var row_count = grid.RowDefinitions.Count;

            var column_count = grid.ColumnDefinitions.Count;

            #region 支持grid cell元素与边框距离设置,但是方法是将cell内元素放到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);

                var settingThickness = GetGridLineThickness(grid);

                Thickness thickness = new Thickness(settingThickness / 2);

                if (row == 0)

                    thickness.Top = settingThickness;

                if (row + rowspan == row_count)

                    thickness.Bottom = settingThickness;

                if (column == 0)

                    thickness.Left = settingThickness;

                if (column + columnspan == column_count)

                    thickness.Right = settingThickness;

                var border = new Border()

                {

                    BorderBrush = GetGridLineBrush(grid),

                    BorderThickness = thickness,

                    Padding = new Thickness(20)

                };

                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);

            }

            #endregion

        }

    }






转自:http://www.cnblogs.com/baijiakai/archive/2012/08/23/2652158.html​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值