• Grid画边框


     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) =>
                    {
                        //改进后的做法,不是简单地根据行和列,而是根据Grid的顶层子控件的个数去添加边框,同时考虑合并的情况
                        var controls = grid.Children;
                        var count = controls.Count;
    
                        for (int i = 0; i < count; i++)
                        {
                            var item = controls[i] as FrameworkElement;
                            var border = new Border()
                            {
                                BorderBrush = new SolidColorBrush(Colors.LightGray),
                                BorderThickness = new Thickness(1)
                            };
    
                            var row = Grid.GetRow(item);
                            var column = Grid.GetColumn(item);
                            var rowspan = Grid.GetRowSpan(item);
                            var columnspan = Grid.GetColumnSpan(item);
    
                            Grid.SetRow(border, row);
                            Grid.SetColumn(border, column);
                            Grid.SetRowSpan(border, rowspan);
                            Grid.SetColumnSpan(border, columnspan);
    
    
                            grid.Children.Add(border);
    
                        }
    
                    };
                }
    
    
            }
    
        }
  • 相关阅读:
    localStorage溢出问题
    面试题梳理
    灵活的楼梯导航条代码
    日期初始化
    css 控制横向布局,超出隐藏,滚动
    自定义滚动条样式
    jquery源码之JQ对象
    jqeury源码之变量解析
    jquery源码之通篇概要
    工作站流处理
  • 原文地址:https://www.cnblogs.com/xibei666/p/4397457.html
Copyright © 2020-2023  润新知