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

如何找枪手做网站国内十大搜索引擎

如何找枪手做网站,国内十大搜索引擎,张北网站建设,设计服务网站一、目的&#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.yidumall.com/news/68121.html

相关文章:

  • wordpress隐藏式列表windows优化大师卸载
  • 哪个网站能在线做司考题目百度精简版网页入口
  • 网站 app 哪个先做网上找客户有什么渠道
  • 二维码怎么做网站浙江seo外包费用
  • 做网站的怎么赚钱网站seo优化有哪些方面
  • 网站建设 联系我们汕头seo网站推广
  • 重庆营销网站建设公司广告营销方式有哪几种
  • 网站开发书籍郑州网站运营
  • 网站建设公司推广方案站长统计app网站
  • 建立网站定制cps广告联盟平台
  • 手机端网站建站公司网站设计哪家好
  • 西安跨境电商平台网站网络营销是什么意思?
  • 做的网站怎样评估价值seo排名快速刷
  • 北京网站改版多少钱网页设计与制作教程
  • php不用框架怎么做网站搜索引擎公司排名
  • 网站建设单页面推广模板怀化seo推广
  • 做哪一类网站能赚钱老客外链
  • 上海网页制作服务商seo效果检测步骤
  • 深圳seo教程百度搜索关键词排名优化技术
  • 武汉做网站及logo的公司seo怎么优化网站排名
  • h5 响应式手机网站友情链接多少钱一个
  • 网站做移动适配电商培训心得体会
  • 搭建cms网站免费网站推广软件下载
  • 个体户可以做开发网站业务嘛站长之家whois查询
  • 有做lol直播网站有哪些免费推广平台
  • 020网站开发建站系统有哪些
  • html5手机网站模板seo优化外包公司
  • 网站建设 asp 武汉百度上的广告多少钱一个月
  • 外贸seo网站推广网络营销方案总结
  • 大连做网站排名专业搜索引擎seo技术公司