您现在的位置是:首页 > 文章详情

C#(WPF)为Grid添加实线边框。

日期:2018-08-07点击:432
原文: C#(WPF)为Grid添加实线边框。

相信大家在做WPF项目的时候,都会用到Grid这个布局控件,一般情况下,如果只是为了布局,那就不需要显示它的边框,但是也有特殊需求,如果把它当做表格来使用,那就需要为它添加实线边框。下面帖代码:


using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

 

namespace WPFStart
{
    ///
    /// 为Grid添加的一个特殊功能,实线边框
    ///
    public class GridHelper
    {
        //边框的宽度
        static double myBorderWidth = 1;

        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(GridHelper), new PropertyMetadata(OnBorderWidthChanged));

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

        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 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.White);

                        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, 0, 0);
                        else if (column == grid.ColumnDefinitions.Count - 1)
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth, myBorderWidth, 0);
                        else
                            border.BorderThickness = new Thickness(myBorderWidth, myBorderWidth,0, 0 );

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

在项目中新建这个类,在XAML页面引用设置属性时,需要添加 “xmlns:ext="clr-namespace:WPFGrid(项目名称)"“,“ ext:GridHelper.ShowBorder="true"”这里就是设置边框是否显示了。


******WPF XAML技术交流群:477319939******

原文链接:https://yq.aliyun.com/articles/677955
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章