WPF-Grid布局控件增加边框附加属性

需要对WPF中的附加属性有所了解。

  • 定义一个静态方法GridEx
namespace Test.Extensions
{
    public static class GridEx
    {
        #region 字段
        /// 
        /// 要操作的Grid
        /// 
        private static Grid grid = null;

        /// 
        /// 默认边框的宽度
        /// 
        private static double defaultBorderWidth = 0.8;
        /// 
        /// 边框颜色
        /// 
        private static SolidColorBrush defaultBorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff202020"));

        #endregion

        #region 控制边框显示
        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(GridEx), new PropertyMetadata(OnShowBorderChanged));

        #endregion

        #region 边框宽度
        public static double GetBorderWidth(DependencyObject obj)
        {
            return (double)obj.GetValue(BorderWidthProperty);
        }

        public static void SetBorderWidth(DependencyObject obj, double value)
        {
            obj.SetValue(BorderWidthProperty, value);
        }

        public static readonly DependencyProperty BorderWidthProperty =
            DependencyProperty.RegisterAttached("BorderWidth", typeof(double), typeof(GridEx), new PropertyMetadata(OnBorderWidthChanged));

        private static void OnBorderWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            defaultBorderWidth = (double)e.NewValue;
        }
        #endregion

        #region 边框颜色
        public static SolidColorBrush GetBorderBrush(DependencyObject obj)
        {
            return (SolidColorBrush)obj.GetValue(BorderBrushProperty);
        }

        public static void SetBorderBrush(DependencyObject obj, SolidColorBrush value)
        {
            obj.SetValue(BorderBrushProperty, value);
        }
        // Using a DependencyProperty as the backing store for BorderBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderBrushProperty =
            DependencyProperty.RegisterAttached("BorderBrush", typeof(SolidColorBrush), typeof(GridEx), new PropertyMetadata((m, n) =>
            {
                defaultBorderBrush = (SolidColorBrush)n.NewValue;
            }));
        #endregion

        #region 显示边框事件
        private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            grid = d as Grid;
            if ((bool)e.NewValue)
            {
                grid.Loaded -= Grid_Loaded;
                grid.Loaded += Grid_Loaded;
            }
        }
        private static void Grid_Loaded(object s, RoutedEventArgs args)
        {
            //根据Grid的顶层子控件的个数去添加边框
            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 border = new Border();
                border.BorderBrush = defaultBorderBrush;

                if (row == grid.RowDefinitions.Count - 1 && column == grid.ColumnDefinitions.Count - 1)
                    border.BorderThickness = new Thickness(defaultBorderWidth);
                else if (row == grid.RowDefinitions.Count - 1)
                    border.BorderThickness = new Thickness(defaultBorderWidth, defaultBorderWidth, 0, defaultBorderWidth);
                else if (column == grid.ColumnDefinitions.Count - 1)
                    border.BorderThickness = new Thickness(defaultBorderWidth, defaultBorderWidth, defaultBorderWidth, 0);
                else
                    border.BorderThickness = new Thickness(defaultBorderWidth, defaultBorderWidth, 0, defaultBorderWidth);

                Grid.SetRow(border, row);
                Grid.SetColumn(border, column);
                Grid.SetRowSpan(border, rowspan);
                Grid.SetColumnSpan(border, columnspan);
                grid.Children.Add(border);
            }
        } 
        #endregion
    }
 }
  • 在xaml中引用,引入类所在的命名空间
xmlns:ex="clr-namespace:Test.Extensions"
  • 附加到定义的Grid控件上
 
        
            
            
        
        
            
            
        
        
        
        
        
    
  • 效果

WPF-Grid布局控件增加边框附加属性_第1张图片

你可能感兴趣的:(wpf)