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
        }
    }

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

                                                      my:GridHelper.GridLineBrush="#FF0F7CC4"
                                                  Width="500"  x:Name="GridZong" >

my为引用命名空间

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