当前位置: 首页 > news >正文

泰安网站建设538sw网站目录程序

泰安网站建设538sw,网站目录程序,长宁区网站建设网,抚松做网站一、目的&#xff1a;通过重写DataGrid的OnAutoGeneratingColumn方法实现根据定义特性自动生成列头信息功能 二、实现 <DataGrid ItemsSource"{local:GetStudents Count50}"/>实体定义如下 public class Student{[DataGridColumn("*")][Display(Na…

一、目的:通过重写DataGrid的OnAutoGeneratingColumn方法实现根据定义特性自动生成列头信息功能


二、实现

<DataGrid ItemsSource="{local:GetStudents Count=50}"/>

实体定义如下 

 public class Student{[DataGridColumn("*")][Display(Name = "姓名", GroupName = "基础信息")][Required]public string Name { get; set; }[DataGridColumn("*")][Display(Name = "班级", GroupName = "基础信息")][Required]public string Class { get; set; }[DataGridColumn("2*")][Display(Name = "地址", GroupName = "基础信息")][Required]public string Address { get; set; }[DataGridColumn("2*")][Display(Name = "邮箱", GroupName = "基础信息")][Required]public string Emall { get; set; }[Display(Name = "可用", GroupName = "其他信息")][Required]public bool IsEnbled { get; set; }[Display(Name = "时间", GroupName = "其他信息")][Required]public DateTime Time { get; set; }[Display(Name = "年龄", GroupName = "基础信息")][Required]public int Age { get; set; }[Display(Name = "分数", GroupName = "成绩信息")]public double Score { get; set; }[DataGridColumn("2*")][Display(Name = "电话号码", GroupName = "基础信息")][Required][RegularExpression("^1[3|4|5|7|8][0-9]{9}$", ErrorMessage = "手机号码不合法!")]public string Tel { get; set; }}

通常如上定义会生成一个根据属性名称列头的表格

三、环境

VS2022

四、示例

自定义一个DislayDataGrid,重写OnAutoGeneratingColumn方法

    public class DislayDataGrid : DataGrid{protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e){base.OnAutoGeneratingColumn(e);if (e.PropertyDescriptor is PropertyDescriptor descriptor)e.Column.Header = descriptor.Attributes.OfType<DisplayAttribute>()?.FirstOrDefault().Name;}}

此时会根据读取特性生成中文名称列头

 

同理其他属性也可以这样设置

 我们扩展一个特性DataGridColumnAttribute

    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]public class DataGridColumnAttribute : Attribute, IDataGridColumn{public DataGridColumnAttribute(string width){DataGridLengthConverter converter = new DataGridLengthConverter();this.Width = (DataGridLength)converter.ConvertFromString(width);}public DataGridColumnAttribute(){}public DataGridLength Width { get; set; } = DataGridLength.Auto;public Type Template { get; set; } = typeof(DataGridTextColumn);/// <summary>/// "{0}.Property"/// </summary>public string PropertyPath { get; set; } = "{0}";public virtual DataGridColumn GetDataGridColumn(PropertyInfo propertyInfo){DataGridColumn dataGridColumn = Activator.CreateInstance(this.Template) as DataGridColumn;if (dataGridColumn == null){if (propertyInfo.PropertyType == typeof(bool)){return new DataGridCheckBoxColumn() { Width = this.Width, IsReadOnly = false };}else if (propertyInfo.PropertyType.IsEnum){return new DataGridComboBoxColumn() { Width = this.Width, IsReadOnly = false };}else{return new DataGridTextColumn() { Width = this.Width, IsReadOnly = false };}}dataGridColumn.Width = this.Width;return dataGridColumn;}}

在生成列时应用该特性

此时列宽也根据特性变化

如果不想通过自定义DisplayDataGrid实现也可以用Behavior行为的方式实现

实现方式如下:

    <DataGrid ItemsSource="{h:GetStudents Count=50}"><b:Interaction.Behaviors><h:DataGridAutoColumnBehavior Type="{x:Type h:Student}"/></b:Interaction.Behaviors></DataGrid>

DataGridAutoColumnBehavior的具体实现方式如下

WPF-Control/Source/Extensions/H.Extensions.Behvaiors/DataGrid/DataGridAutoColumnBehavior.cs at main · HeBianGu/WPF-Control · GitHub

这种方式在自动化生成表格数据中非常有用 

五、需要了解的知识点

DataGrid.OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs) 方法 (System.Windows.Controls) | Microsoft Learn

DataGrid 类 (System.Windows.Controls) | Microsoft Learn

六、源码地址

GitHub - HeBianGu/WPF-ControlDemo: 示例

GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库

GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库

七、了解更多

System.Windows.Controls 命名空间 | Microsoft Learn

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

http://www.yayakq.cn/news/793196/

相关文章:

  • 延边北京网站建设沈阳网站制作列表网
  • 网站开发工程师绩效wordpress如何导入模板数据库
  • 布料市场做哪个网站好网站建设与维护是什么内容
  • 国外调色网站投资公司起名
  • 特色食品网站建设策划书福建网站优化
  • 网站建设网络公司哪里可以找到做网站的
  • 网站规划与建设ppt最好的包装设计公司哪家好
  • 网站建设及推广话术上海网络平台有哪些
  • 哪个网站能把图片拼凑起来做gif的项目建设调研报告
  • 做印刷的网站wordpress媒体库地址
  • 咸宁有做网站的吗建站网站排行
  • 有没有什么做h5的网站网站建设工作总结培训
  • 网站设计 论坛哪里有做网站公司
  • 长春网站运做思路游戏开发课程
  • 工程网站建设方案wordpress在页面中调用文章
  • 简述网站开发平台及常用开发工具网站建设 域名 服务器
  • 腾讯云wordpress建站个人印章在线制作网站
  • 景区网站如何建设郑州网站建设 个人工作室
  • 内部网站建设、快速开发安卓app软件
  • app 网站开发公司电话wordpress 即将跳转
  • 两学一做山西答题网站十堰秦楚网公众号
  • 微信官方网站建设张家港做网站的公司
  • 网站建设电商代运营iis做网站
  • 建网站基础知识网站建设有什么费用
  • 网站外链建设可以提升网站权重对吗菏泽 网站建设
  • 建设微信网站要多少钱凡客精选
  • 手机支付网站开发网站改版公司哪家好
  • 百度关键词seo推广合肥优化推广公司
  • 在国外服务器上做网站项目如何赚钱网络考试
  • 在沈阳做一个展示网站多少钱ui设计前景怎样