2017-02-01 2 views
1

私はGrid.Mvc Frameworkを使用してモデルデータを提示しています。Grid.Mvcをヘッダーソースとしてressourceファイルで拡張する

を見てみましょう。箱から出してSource CodeDocumentation

まず列ヘッダーを提示する2つのオプションがあります。ressourceファイルがない

が..

//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() 

第二:ビューで

使用Ressourceストリングス..

//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が属性は、グリッドが私Ressourceファイル内の列のタイトルを探して作るべき「DisplayFieldNames」

答えて

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; } 
    } 
} 
関連する問題