DataAnnotations Support for WinForms

折月煮酒 提交于 2020-02-15 08:16:53

问题


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
    • [Display(AutoGenerateField=false)]
      • Do not display this column
    • [Display(Order=N)]
      • Display this column as the nth column in the grid
  • DisplayColumn
    • [DisplayColumn("ColumnName")]
      • If this object is a property of another object, display this column value instead of the object type
  • Display Format
    • [DisplayFormat(DataFormatString="{0:formatstring}")]
      • Format the data using the specified format string
  • DataType
    • [DataType(DataType.Currency)]
      • Display the data as a currency in the default local currency format

Example

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; }
}

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

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

Documentation

The documentation declares that it is supported in ASP.NET and ASP.NET MVC.

System.ComponentModel.DataAnnotations Namespace

The System.ComponentModel.DataAnnotations namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.

Questions
It seems that these classes have not been adopted/supported in the windows forms environment. Is this true?

Is there a simple way to implement support for the data annotations in WinForms?

Is there a simple way to annotate the classes/entities that can be used to format the display of a DataGridView and/or UltraGrid?


回答1:


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.

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




回答2:


DataAnnotations attributes for DataGridView in Windows Forms

There is no builtin support for Data Annotation in Windows Forms, but knowing how the attributes work and how windows forms work, we can use them in windows forms.

Here in this post, I'll show an extension method for DataGridView which binds an IList<T> to DataGridView and auto-generate columns based on the data annotations attributes, so you can get the following DataGridView, by calling dataGridView1.Bind(list);:

Look at the following items which are coming from data annotations attributes:

  • Column visibility: Id column is invisible
  • Column header texts: They are custom texts different from property names
  • Order of columns: The order of columns is custom, different from property orders
  • Tooltip: We have shown custom tooltip for columns.
  • Format of data: We have used custom format for date.

And many more things still you can have using attributes.

While the model is like this:

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

[TypeDescriptionProvider(typeof(MetadataTypeTypeDescriptionProvider))]
public class Person
{
    [Display(Name = "Id")]
    [Browsable(false)]
    public int? Id { get; set; }

    [Display(Name = "First Name", Description = "First name.", Order = 1)]
    public string FirstName { get; set; }

    [Display(Name = "Last Name", Description = "Last name", Order = 2)]
    public string LastName { get; set; }

    [Display(Name = "Birth Date", Description = "Date of birth.", Order = 4)]
    [DisplayFormat(DataFormatString = "yyyy-MM-dd")]
    public DateTime BirthDate { get; set; }

    [Display(Name = "Homepage", Description = "Url of homepage.", Order = 5)]
    public string Url { get; set; }

    [Display(Name = "Member", Description = "Is member?", Order = 3)]
    public bool IsMember { get; set; }
}

Bind<T> Extension Method

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows.Forms;

public static class DataGridViewExtensions
{
    public static void Bind<T>(this DataGridView grid, IList<T> data,
        bool auotoGenerateColumsn = true)
    {
        if (auotoGenerateColumsn)
        {
            var properties = TypeDescriptor.GetProperties(typeof(T));
            var metedata = properties.Cast<PropertyDescriptor>().Select(p => new
            {
                Name = p.Name,
                HeaderText = p.Attributes.OfType<DisplayAttribute>()
                    .FirstOrDefault()?.Name ?? p.DisplayName,
                ToolTipText = p.Attributes.OfType<DisplayAttribute>()
                    .FirstOrDefault()?.GetDescription() ?? p.Description,
                Order = p.Attributes.OfType<DisplayAttribute>()
                    .FirstOrDefault()?.GetOrder() ?? int.MaxValue,
                Visible = p.IsBrowsable,
                ReadOnly = p.IsReadOnly,
                Format = p.Attributes.OfType<DisplayFormatAttribute>()
                    .FirstOrDefault()?.DataFormatString,
                Type = p.PropertyType
            });
            var columns = metedata.OrderBy(x => x.Order).Select(m =>
            {
                DataGridViewColumn c;
                if (m.Type == typeof(bool)) {
                    c = new DataGridViewCheckBoxColumn(false); }
                else if (m.Type == typeof(bool?)) {
                    c = new DataGridViewCheckBoxColumn(true); }
                else { c = new DataGridViewTextBoxColumn(); }
                c.DataPropertyName = m.Name;
                c.Name = m.Name;
                c.HeaderText = m.HeaderText;
                c.ToolTipText = m.ToolTipText;
                c.DefaultCellStyle.Format = m.Format;
                c.ReadOnly = m.ReadOnly;
                c.Visible = m.Visible;
                return c;
            });
            grid.Columns.Clear();
            grid.Columns.AddRange(columns.ToArray());
        }
        grid.DataSource = data;
    }
}

DataAnnotations Validation attributes for Windows Forms

Also for supporting data annotations validations you can implement IDataErrorInfo interface using Validator class, the same way that I've done it in DataAnnotations Validation attributes for Windows Forms.

Note

To enhance the answer, you may want to creating a type descriptor which cares about metadata attributes and then decorate the models with that type descriptor. You can start by using the code of AssociatedMetadataTypeTypeDescriptor, MetadataPropertyDescriptorWrapper, AssociatedMetadataTypeTypeDescriptionProvider.

Also you can create a Metadata class and apply the effect of some attributes like Url or DataType on the metadata. Looking into this post will give you some idea: Combining multiple Attributes to a single Attribute - Merge Attributes.



来源:https://stackoverflow.com/questions/32015348/dataannotations-support-for-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!