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

推荐订阅源

P
Palo Alto Networks Blog
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
GbyAI
GbyAI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
量子位
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
CERT Recently Published Vulnerability Notes
Recent Announcements
Recent Announcements
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
NISL@THU
NISL@THU
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
F
Full Disclosure
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
D
Docker
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
Help Net Security
Help Net Security
V
Visual Studio Blog
小众软件
小众软件
B
Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - HotSky

Chrome启用CDP(chrome-devtools-protocol)进行远程操控 WPF 让ScrollViewer支持按住鼠标中键拖拽滚动内容 WPF 让ScrollViewer支持鼠标中键滚动缩放内容 Nodejs使用 nodejs中写sql需要用in时的写法 C#城市最短路径 C#Animation Sqlite PDF附录A: 内容流操作码 WPF FPS类 生命模拟 C# Sql帮助类,可扩展 WPF WriteableBitmap通过GDI+绘制帮助类 Alpha混合 Bmp读写二值图 WPF支持任意快捷键+鼠标组合的绑定类 WPF阻止窗体被系统缩放,使用显示器DPI C#访问或修改私有类、函数、变量、属性 WPF一个简单的属性编辑控件
WPF DataGrid自动增长序号列
HotSky · 2024-06-03 · via 博客园 - HotSky
/// <summary>
/// 自动增长序号列
/// </summary>
public class DataGridRowIndexColumn : DataGridTextColumn
{
    /// <summary>
    /// 可以指定开始序号
    /// </summary>
    public int StartIndex
    {
        get { return (int)GetValue(StartIndexProperty); }
        set { SetValue(StartIndexProperty, value); }
    }

    public static readonly DependencyProperty StartIndexProperty =
        DependencyProperty.Register("StartIndex", typeof(int), typeof(DataGridRowIndexColumn), new FrameworkPropertyMetadata(0, propertyChangedCallback: StartIndexChanged));

    private static void StartIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var column = (DataGridRowIndexColumn)d;
        if (e.OldValue == e.NewValue) return;
        int v = (int)e.NewValue;
        if (column.DataGridOwner == null) return;
        IList list = column.DataGridOwner.Items;
        TextBlock ele = null;
        int i = 0;
        foreach (object item in list)
        {
            ele = column.GetCellContent(item) as TextBlock;
            ele.Text = (v + i).ToString();
            i++;
        }
    }

    protected override void RefreshCellContent(FrameworkElement element, string propertyName)
    {
        base.RefreshCellContent(element, propertyName);
    }

    protected override bool OnCoerceIsReadOnly(bool baseValue)
    {
        return true;
    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        TextBlock text = (TextBlock)base.GenerateElement(cell, dataItem);
        int index = (DataGridOwner.Items as IList).IndexOf(dataItem);
        text.Text = (index + StartIndex).ToString();
        //https://www.cnblogs.com/RedSky/p/18215093
        TypeUtil.InvokeMethod(this, typeof(DataGridColumn), "ApplyStyle",
            new Type[] { typeof(bool), typeof(bool), typeof(FrameworkElement) },
            new object[] { false, false, text });
        return textBox;
    }
}

调用方式:

<DataGrid ItemsSource="{Binding Mods}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <local:DataGridRowIndexColumn Binding="{Binding Sort}" StartIndex="20"/>
        <DataGridTextColumn Binding="{Binding Name}"/>
    </DataGrid.Columns>
</DataGrid>