wpf grid添加边框

/// <summary>
    /// 为Grid添加边框
    /// </summary>
    public class GridHelper
    {
        //边框的宽度
        static double myBorderWidth = 0.5;
        //请注意:可以通过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;

                   
                   
                    if (grid.ColumnDefinitions.Count == 0)
                    {
                        //设置边框线的颜色
                        var border = new Border();
                        border.BorderBrush = new SolidColorBrush(Colors.Gray);
                        border.BorderThickness = new Thickness(myBorderWidth, 0, myBorderWidth, myBorderWidth);
                        Grid.SetRow(border, grid.RowDefinitions.Count - 1);
                        Grid.SetColumn(border, 0);

                        grid.Children.Add(border);
                    }
                    //最后一行
                    for (int k = 0; k < grid.ColumnDefinitions.Count; k++)
                    {

                        var border = new Border();
                        border.BorderBrush = new SolidColorBrush(Colors.Gray);
                        if (k == 0)
                        {
                            border.BorderThickness = new Thickness(myBorderWidth, 0, 0, myBorderWidth);
                        }
                        else if (k == grid.ColumnDefinitions.Count - 1)
                        {
                            border.BorderThickness = new Thickness(0, 0, myBorderWidth, myBorderWidth);
                        }
                        else
                        {
                            border.BorderThickness = new Thickness(0, 0, 0, myBorderWidth);
                        }
                        Grid.SetRow(border, grid.RowDefinitions.Count - 1);
                        Grid.SetColumn(border, k);
                        
                        grid.Children.Add(border);
                    }

                    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 = new SolidColorBrush(Colors.Gray);
                     
                        if (row != grid.RowDefinitions.Count - 1)
                        {
                            if (row == 0 && column == 0)
                            {
                                border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, myBorderWidth, myBorderWidth);
                            }
                            else if (row == 0 && column != 0)
                            {
                                border.BorderThickness = new Thickness(0, myBorderWidth, myBorderWidth, myBorderWidth);
                            }
                            else if (row > 0 && column == 0)
                            {
                                border.BorderThickness = new Thickness(myBorderWidth, 0, myBorderWidth, myBorderWidth);
                            }
                            else if (row > 0 && column > 0)
                            {
                                border.BorderThickness = new Thickness(0, 0, myBorderWidth, myBorderWidth);
                            }

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

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

        }

    }

界面进行如下设置:

 <Grid  x:Name="mesureGrid" Height="404" Width="512" ScrollViewer.CanContentScroll="True"  ext:GridHelper.ShowBorder="true" Grid.Column="1" Grid.Row="1"
                      Background="White" >

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="39*"/>
                    <ColumnDefinition Width="216*"/>
                    <ColumnDefinition Width="199*"/>
                    <ColumnDefinition Width="39*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="29*"/>
                    <RowDefinition Height="27*"/>
                   
                </Grid.RowDefinitions>
                
            </Grid>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值