WinForms的DataAnnotations支持 [英] DataAnnotations Support for WinForms
问题描述
我想在Windows Forms应用程序中的类和实体上使用数据注释。我使用Windows DataGridViews和Infragistics UltraGrids。我以前已经成功地使用 [DisplayName(要显示的名称)]
属性在DataGridView / UltraGrid上设置列标题文本。
I would like to use data annotations on my classes and entities in a Windows Forms application. I use windows DataGridViews and Infragistics UltraGrids. I have previously successfully used the [DisplayName("Name to Display")]
attribute to set the column header text on a DataGridView/UltraGrid.
这非常有益,因为我可能有几个显示此类的网格,而不是将每个网格配置为显示适当的标题文本,我只需设置一个数据注释即可。
This is very beneficial, because I may have several grids displaying this class, and instead of configuring each grid to display the appropriate header text, I can simply set one data annotation.
我也想使用以下数据注释:
I'd like to make use of the following Data Annotations as well:
- 显示
- [Display(AutoGenerateField = false)]
- 不要显示此列
- 将此列显示为网格中的第n列
- [DisplayColumn( ColumnName)]
- 如果此对象是另一个对象的属性对象,显示此列值而不是对象类型
- [DisplayFormat(DataFormatString = {0:formatstring})]
- 格式化数据使用指定格式的字符串
- [DataType(DataType.Currency)]
- 以默认本地货币格式将数据显示为货币
示例
给出以下带注释的数据类:
Given the following annotated data classes:
public class Item { //Specifies that the column should not be displayed [Display(AutoGenerateField = false)] public int ItemID { get; set; } //Specifies that the column should be the 1st column in the datagridview [Display(Order = 1)] public int Name { get; set; } //Specifies that the column should be the 3rd column in the datagridview //Specifies that the column header text should display "Cost" instead of "Price" [Display(Order = 3, Name="Cost")] //Specifies that the column should be rendered using the default local currency format string [DataType(DataType.Currency)] public int Price { get; set; } //Specifies that the column should be the 4th column in the datagridview [Display(Order = 4)] //specifies that the column should be rendered using the datetime format string "M/d/yy h:mm tt" [DisplayFormat(DataFormatString = "{0:M/d/yy h:mm tt")] public DateTime ExpirationDate { get; set; } //Specifies that the column should be the 2nd column in the datagridview [Display(Order = 2)] public ItemCategory Category { get; set; } } //Specifies that the Name column should be displayed, if referenced in a containing object [DisplayColumn("Name")] public class ItemCategory { public int CategoryID { get; set; } public string Name { get; set; } }
我希望DataGridView呈现如下:
I would like the DataGridView to render like this:
+-------+---------------+--------+-----------------+ | Name | Category | Cost | ExpirationDate | +-------+---------------+--------+-----------------+ | Item1 | Category1Name | $30.45 | 7/23/17 5:22 PM | +-------+---------------+--------+-----------------+ | Item2 | Category1Name | $45.05 | 8/24/17 6:22 PM | +-------+---------------+--------+-----------------+ | Item3 | Category2Name | $35.50 | 9/25/17 7:22 PM | +-------+---------------+--------+-----------------+
但是实际上在.Net 4.5.2 WinForms中使用DataGridViews时,datagrid实际显示如下:
However in practice with DataGridViews in .Net 4.5.2 WinForms, the datagrid is actually displayed as follows:
+----+-------+-------+----------------+--------------------+ | ID | Name | Price | ExpirationDate | Category | +----+-------+-------+----------------+--------------------+ | 1 | Item1 | 30.45 | 7/23/17 | Namespace.Category | +----+-------+-------+----------------+--------------------+ | 2 | Item2 | 45.05 | 8/24/17 | Namespace.Category | +----+-------+-------+----------------+--------------------+ | 3 | Item3 | 35.50 | 9/25/17 | Namespace.Category | +----+-------+-------+----------------+--------------------+
文档 >
文档声明它在ASP.NET和ASP.NET MVC中受支持。
The documentation declares that it is supported in ASP.NET and ASP.NET MVC.
System.ComponentModel.DataAnnotations命名空间
System.ComponentModel.DataAnnotations命名空间提供了用于定义ASP.NET MVC和ASP.NET数据控件的元数据的属性类。
The System.ComponentModel.DataAnnotations namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.
问题
似乎Windows窗体环境中没有采用/支持这些类。是真的吗?Questions
It seems that these classes have not been adopted/supported in the windows forms environment. Is this true?是否有一种简单的方法来实现对WinForms中数据注释的支持?
Is there a simple way to implement support for the data annotations in WinForms?
是是否有一种简单的方法来注释可用于格式化DataGridView和/或UltraGrid的显示的类/实体?
Is there a simple way to annotate the classes/entities that can be used to format the display of a DataGridView and/or UltraGrid?
推荐答案
Infragistics UltraGrid和Windows DataGridView都不以这种方式支持数据注释。从15.1开始的DevExpress在其数据布局控件中仅对WinForms数据注释提供了有限的支持 https://community.devexpress.com/blogs/thinking/archive/2015/06/08/winforms-data-layout-control-data-annotation-attributes .aspx 。
Neither Infragistics UltraGrid nor the Windows DataGridView support data annotations in this manner. DevExpress as of 15.1 has some limited WinForms data annotation support within its Data Layout Control https://community.devexpress.com/blogs/thinking/archive/2015/06/08/winforms-data-layout-control-data-annotation-attributes.aspx.
我找到了此链接,有人在其中询问有关如何通过查看元数据来抑制Infragistics Grid中的列的类似问题: http://www.infragistics.com/community/forums/t/91864.aspx
I found this link where someone is asking similar questions about how to suppress columns within Infragistics Grid by looking at metadata: http://www.infragistics.com/community/forums/t/91864.aspx
这篇关于WinForms的DataAnnotations支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- [Display(AutoGenerateField = false)]