Silverlight Grid表格

  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 BackGroudBrush
        public static readonly DependencyProperty GridBackGroudBrushProperty =
       DependencyProperty.RegisterAttached("GridBackGroudBrush", typeof(Brush), typeof(GridHelper),
       new PropertyMetadata(OnGridBackGroudBrushChanged));
        private static void OnGridBackGroudBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

        #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 ? new SolidColorBrush(Colors.Red) : brush;
        }
        public static void SetGridLineBrush(DependencyObject obj, Brush value)
        {
            obj.SetValue(GridLineBrushProperty, value);
        }
        private static void OnGridLineBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

 

        public static Brush GetGridBackGroudBrush(DependencyObject obj)
        {
            Brush brush = (Brush)obj.GetValue(GridBackGroudBrushProperty);
            return brush == null ? new SolidColorBrush(Colors.Transparent) : brush;
        }
        public static void SetGridBackGroudBrush(DependencyObject obj, Brush value)
        {
            obj.SetValue(GridBackGroudBrushProperty, value);
        }


        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 va = item as Border;
                var border = new Border();
                if (va == null)
                {
                    border.BorderBrush = GetGridLineBrush(grid);
                    border.Background = GetGridBackGroudBrush(grid);
                    if (row == 0)
                    {
                        if (column == 0)
                            border.BorderThickness = new Thickness(settingThickness, settingThickness, settingThickness, settingThickness);
                        else
                            border.BorderThickness = new Thickness(0, settingThickness, settingThickness, settingThickness);
                    }

                    else
                    {
                        if (column == 0)
                            border.BorderThickness = new Thickness(settingThickness, 0, settingThickness, settingThickness);
                        else
                            border.BorderThickness = new Thickness(0, 0, settingThickness, settingThickness);
                    }

                }
                else
                {
                    border = va;
                }

                grid.Children.RemoveAt(i);
                Grid.SetRow(border, row);
                Grid.SetColumn(border, column);
                Grid.SetRowSpan(border, rowspan);
                Grid.SetColumnSpan(border, columnspan);


                if (border.Child == null)
                {
                    border.Child = item;
                }
                else
                {
                    border = item as Border;
                }

 

                grid.Children.Insert(i, border);
            }
            #endregion
        }
    }

表格内必须要有控件来填充

 <Grid    my:GridHelper.ShowBorder="True"                                                     my:GridHelper.GridLineThickness="1"  
                                                    my:GridHelper.GridLineBrush="#FF0F7CC4"
                                                  Width="500"  x:Name="GridZong" >

</Grid>

my为引用命名空间

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值