1

我正在使用Grid.Mvc 框架来展示我的模型数据。

看:源代码文档

开箱即用有两个选项来显示列标题

第一的:

没有资源文件..

//Annotation
[GridColumn(Title = "Active Foo?")]
public bool Enabled { get; set; }


[GridColumn(Title = "Date", Format = "{0:dd/MM/yyyy}")]
public DateTime FooDate { get; set; }

...

//Display the Model items with assigned Column Titles
@Html.Grid(Model).AutoGenerateColumns()

第二:

在视图中使用资源字符串..

//Assign Column Header from 
@Html.Grid(Model).Columns(columns =>
{
        columns.Add(n => n.Enabled).Titled(DisplayFieldNames.Enabled); 
        columns.Add(n => n.FooDate).Titled(DisplayFieldNames.FooDate);
})

我想知道如何扩展第一种方法(在模型中使用数据注释)

就像是:

[GridColumn(Title ="Enabled", ResourceType = typeof(DisplayFieldNames))]

[GridColumn(Title = "Date", ResourceType = typeof(DisplayFieldNames), Format = "{0:dd/MM/yyyy}")]

里面的ResourceType属性应该使网格在我的资源文件“DisplayFieldNames”中查找列标题

4

1 回答 1

0

在朋友的支持下,我找到了自己的解决方案。

在这里,希望它对其他人也有帮助。

public class LocalizedGridCoulmnAttribute : GridColumnAttribute
{

    private Type _resourceType;

    /// <summary>
    /// The type of the Ressource file 
    /// </summary>
    public Type ResourceType
    {
        get
        {
            return this._resourceType;
        }
        set
        {
            if (this._resourceType != value)
            {
                ResourceManager rm = new ResourceManager(value);
                string someString = rm.GetString(LocalizedTitle);

                this._resourceType = value ?? value;
            }
            if (ResourceType != null && LocalizedTitle != String.Empty)
            {
                ResourceManager rm = new ResourceManager(ResourceType);
                Title = rm.GetString(LocalizedTitle);
            }
        }
    }

    /// <summary>
    /// Overrides The Title Property of GridColumnAttribute
    /// Works with Ressourcefile
    /// </summary>
    public string LocalizedTitle
    {
        set {
            if (ResourceType != null && value != String.Empty)
            {
                ResourceManager rm = new ResourceManager(ResourceType);
                Title = rm.GetString(value);
            }
            else Title = value ?? value;
        }
        get { return Title; }
    }
}
于 2017-02-01T18:58:18.443 回答