惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
T
Tenable Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Scott Helme
Scott Helme
C
Cisco Blogs
T
Tor Project blog
P
Privacy International News Feed
Forbes - Security
Forbes - Security
S
Schneier on Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy & Cybersecurity Law Blog
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
T
Threatpost
S
Security @ Cisco Blogs
H
Heimdal Security Blog
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
N
News and Events Feed by Topic
Hugging Face - Blog
Hugging Face - Blog
T
Troy Hunt's Blog
WordPress大学
WordPress大学
腾讯CDC
V
V2EX
IT之家
IT之家
P
Proofpoint News Feed
S
Securelist
Hacker News: Ask HN
Hacker News: Ask HN
T
Threat Research - Cisco Blogs
爱范儿
爱范儿
雷峰网
雷峰网
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Cloudflare Blog
美团技术团队
月光博客
月光博客
博客园 - Franky
小众软件
小众软件
V
Vulnerabilities – Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志

博客园 - Louis.Lu.Sz

在C#中,如果声明字段时不加关键字volatile,会影响多线程环境中对该字段的访问吗? Win10, Win11 Ping不通 FastReport使用笔记 Windows窗体控件库的小秘密 在windows桌面显示IP等信息的小工具分享 oracle,根据查询结果结构创建新表 Oracle多表关联如何更新多个字段 我想实现一个通用的配置读写类 【转】Android程序右上角不显示3个点的菜单 Visual studio项目调试时提示“ 你正在调试XXXX的发布版本。” 【原】记录一下第一次使用Python简单处理Excel 【原创】分享一种WPF列表数据的分页打印方案 [原创] 分享一种Asp.NetMVC WebApi作为后端技术结合Vue前端框架开发时开发环境的优雅配置方案 [原] c# winform controls 查找指定类型子控件的扩展方法 [转]Errors while building APK. You can find the errors in the 'Messages' view.解决办法 [转]oracle数据库转mysql数据库 SaveFileDialog下载模板文件 算法:把一个数字拆分成指定数字的和,允许数字个数为0和重复 WPF简单实现可以左右滑动的CheckBox复选框,样式模仿的微信
WPF里借助附加属性让DataGrid显示行号
Louis.Lu.Sz · 2020-04-14 · via 博客园 - Louis.Lu.Sz

大部分表格,都有显示行号的需求。

WPF里的DataGrid显示行号,一般如下方式显示:

 在后台代码里写上相应的事件处理方法:

 

运行效果如下:

 但是,这样需要对项目里的所有需要显示行号的DataGrid都要写重复的代码。

十分不优雅。

下面介绍一种通过附加属性的方式,把上面的代码进行一下封装。

然后每次应用的时候,只需要简单的给DataGrid设置一下属性即可。

#region DisplayRowNumber
        public static bool GetDisplayRowNumber(DependencyObject obj)
        {
            return (bool)obj.GetValue(DisplayRowNumberProperty);
        }

        [AttachedPropertyBrowsableForType(typeof(DataGrid))]
        public static void SetDisplayRowNumber(DependencyObject obj, bool value)
        {
            obj.SetValue(DisplayRowNumberProperty, value);
        }

        /// <summary>
        /// 设置是否显示行号
        /// </summary>
        public static readonly DependencyProperty DisplayRowNumberProperty =
            DependencyProperty.RegisterAttached("DisplayRowNumber", 
                                                typeof(bool), 
                                                typeof(DataGridHelper), 
                                                new PropertyMetadata(false, OnDisplayRowNumberChanged));

        private static void OnDisplayRowNumberChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGrid grid = d as DataGrid;
            if (grid == null)
            {
                return;
            }

            if ((bool)e.NewValue)
            {
                grid.LoadingRow += OnGridLoadingRow;
                grid.UnloadingRow += OnGridUnloadingRow;
            }
            else
            {
                grid.LoadingRow -= OnGridLoadingRow;
                grid.UnloadingRow -= OnGridUnloadingRow;
            }
        }

        private static void RefreshDataGridRowNumber(object sender)
        {
            DataGrid grid = sender as DataGrid;
            if (grid == null)
            {
                return;
            }

            foreach (var item in grid.Items)
            {
                var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
                if (row == null)
                {
                    return;
                }
                row.Header = row.GetIndex() + 1;
            }
        }

        private static void OnGridUnloadingRow(object sender, DataGridRowEventArgs e)
        {
            RefreshDataGridRowNumber(sender);
        }

        private static void OnGridLoadingRow(object sender, DataGridRowEventArgs e)
        {
            RefreshDataGridRowNumber(sender);
        }

        #endregion

总结,主要运用了WPF中附加属性的技术。