列宽的数据注释 [英] Data Annotation for column width

查看:18
本文介绍了列宽的数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一组对象绑定到 DevExpress GridControl,并使用 15.1 数据注释 自定义外观.但是,我很难找到有关设置属性列大小的任何信息.这可以通过注释实现吗?

I'm binding a collection of objects to a DevExpress GridControl, and using 15.1 Data Annotations to customise the look. However I'm struggling to find anything about setting column size of a property. Is this possible through annotations?

带注释的类:

public class DataFeedback
{
    [Display(Name = "Row Num", Order = 0)]
    public int RowNum { get; set; }
    [Display(Name = "Description", Order = 1)]
    public string Desc { get; set; }

    public DataFeedback(int rowNum, string desc)
    {
        RowNum = rowId;
        Desc = desc;
    }
}

简单绑定

var feedbackList = new List<DataFeedback>()
feedbackList.Add(new DataFeedback(1, "test"))
gridControl1.DataSource = feedbackList;

// only layout I've found so far
gridView1.BestFitColumns();

推荐答案

没有现成的数据注释属性可用于指定 UI 网格的列大小.StringLength 属性(和其他属性)用于指定数据库中的列大小和用于数据验证的数据大小,但仅此而已.

Out of the box there are no data annotation attributes that can be used to specify the column size of the UI grid. The StringLength attribute (and others) are used to specify the column size in the database and the size of the data for data validation, but that's as far as they'll go.

我不熟悉 DevExpress 控件,但如果它提供了自动列生成过程的挂钩,您可以执行类似于我为 Telerik 网格所做的操作(http://geekswithblogs.net/sdorman/archive/2015/11/05/kendo-grid-mvc-wrapper-automatic-column-configuration.aspx).

I'm not familiar with the DevExpress controls, but if it provides a hook in to the automatic column generation process you can do something similar to what I did for the Telerik grid (http://geekswithblogs.net/sdorman/archive/2015/11/05/kendo-grid-mvc-wrapper-automatic-column-configuration.aspx).

在那个解决方案中,我创建了一个自定义数据注释属性(与此类似):

In that solution, I created a custom data annotations attribute (similar to this):

public class GridColumnAttribute : Attribute, IMetadataAware
{
    public const string Key = "GridColumnMetadata";

    public string Width { get; set; }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues[GridColumnAttribute.Key] = this;
    }
}

然后,你装饰你的视图模型:

Then, you decorate your view model:

public class DataFeedback
{
    [Display(Name = "Row Num", Order = 0)]
    [GridColumn(Width = "100px")]
    public int RowNum { get; set; }

    [Display(Name = "Description", Order = 1)]
    [GridColumn(Width = "300px")]
    public string Desc { get; set; }   
 }

最后,在从列生成挂钩调用的代码中,您将执行类似以下操作:

Finally, in the code that gets called from your column generation hook, you would do something similar to this:

public static void ConfigureColumn<T>(GridColumnBase<T> column) where T : class
{
   CachedDataAnnotationsModelMetadata metadata = ((dynamic)column).Metadata;
   object attributeValue = null;
   if (metadata.AdditionalValues.TryGetValue(GridColumnAttribute.Key, out attributeValue))
   {
      var attribute = (GridColumnAttribute)attributeValue;
      column.Width = attribute.Width;
   }
}

看起来您可以通过使用支持的流畅 API 和 With<T> 扩展方法和/或可能连接到 RowCellStyle 来做到这一点事件.(https://documentation.devexpress.com/#WindowsForms/CustomDocument18017)

It looks like you might be able to do this by using the supported fluent API and the With<T> extension method and/or possibly hooking in to the RowCellStyle event. (https://documentation.devexpress.com/#WindowsForms/CustomDocument18017)

如果您无法连接到列生成过程,您可以做同样的事情,但使用您自己的扩展方法,该方法在网格绑定后被调用,就像您调用 BestFitColumns().

If you can't hook in to the column generation process, you may be able to do the same thing but using your own extension method that gets called after the grid is bound, much like you're doing with calling BestFitColumns().

这篇关于列宽的数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆