首页 > 其他分享 >WPF DataGrid自动增长序号列

WPF DataGrid自动增长序号列

时间:2024-06-03 16:23:45浏览次数:12  
标签:return int column DataGrid TextBlock bool typeof 序号 WPF

/// <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>

 

标签:return,int,column,DataGrid,TextBlock,bool,typeof,序号,WPF
From: https://www.cnblogs.com/RedSky/p/18229106

相关文章

  • WPF RenderTransform TransformGroup ScaleTransform TranslateTransform
    <Windowx:Class="WpfApp132.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • Wpf 使用 Prism 开发MyToDo应用程序
    MyToDo是使用WPF,并且塔配Prism框架进行开发的项目。项目中进行了前后端分离设计,客户端所有的数据均通过API接口获取。适合新手入门学习WPF以及Prism框架使用。首页统计以及点击导航到相关模块功能待办事项增删改查功能备忘录增删改查功能登录注册功能系统设备主题颜......
  • wpf 中阿里图标库的使用
    阿里图标库的使用阿里图标库iconfont1.进入阿里图标库主界面后,根据需要搜索自己要用的图标,然后加入到购物车中2.打开“资源管理-我的项目”,进入我的项目界面,然后点击"下载至本地",把资源文件下载到本地3.引用资源(通过编码"&#xefdc"和ttf字体文件实现效果)效果......
  • C#WPF数字大屏项目实战06--报警信息
    1、ItemsControl简介 ItemsControl是用来表示一些条目集合的控件,所以它叫条目控件,它的成员是一些其它控件的集合,其继承关系如下:     其常用的派生控件为:ListBox、ListView、ComboBox,为ItemsControl的具体实现。   ItemsControl的成员条目可以为不同的类型,如自......
  • WPF快速创建绑定属性
    快速添加一个绑定属性,例如privatedouble_CurrentFontSize=12;publicdoubleCurrentFontSize{get{return_CurrentFontSize;}set{_CurrentFontSize=value;OnPropertyChanged("Cu......
  • Avalonia下DataGrid多选MVVM绑定的功能
    安装Avalonia.Xaml.BehaviorsInstall-PackageAvalonia.Xaml.BehaviorsDataGridSelectedItemsBehavior.csusingAvalonia;usingAvalonia.Controls;usingAvalonia.Threading;usingAvalonia.Xaml.Interactivity;namespaceCgdataBase;publicclassDataGridSelected......
  • WPF MVVM如何在ViewModel直接操作控件对象
    早些年在WPF中使用COM组件时,需要在ViewModel中操作COM组件中的控件对象,但是这个控件对象又不支持绑定,后面的解决办法是在窗口加载时,将控件对象以参数传递到Loaded事件的处理命令中,然后将这个对象记录下来,后面就可以直接操作这个控件了。今天同事在使用WebView2的时候,又遇到这个......
  • .NET|--WPF|--如何使用LINQPad创建一个WPF示例
    1.安装包管理器#搜索框内需要填入↓"id=Microsoft.NETCore.App""id=Microsoft.WindowsDesktop.App.Ref"2.代码voidMain(){ varapp=newSystem.Windows.Application(); varmainWindow=newSystem.Windows.Window { Title="SimpleWPFProgra......
  • 原生WPF使用IOC容器
    1、删除App.xaml中的StartupUri=""2、改造App.xaml.cspublicpartialclassApp:Application{publicApp(){ServiceProvider=GetServiceProvider();}privateIServiceProviderGetServiceProvider(){ServiceC......
  • WPF使用事件聚合器,实现任意页面跨页通信
    前言:最近几天有好几个小伙伴玩WPF,遇到不同页面,不知道要怎么传递消息。于是,我今天就来演示一个事件聚合器的玩法,采用prism框架来实现。作为福利,内容附带了主页面打开对话框时候直接通过参数传递消息的一个小例子,具体请自行围观。以下内容,创建wpf项目以及引用prism和实现依赖注入......