2014-02-14 23 views
0

我似乎無法找到任何地方,任何有關如何使用GroupFormatter委託的示例,以便在使用ObjectListView控件時添加頁腳到我的組。如何使用GroupFormatter和ObjectListView控件

有沒有人有任何可以證明這一點的例子?我想從組標題中刪除文本並添加一個頁腳(每個頁腳不同的文本)。以及更改字體等。

任何示例都會非常有幫助。

+0

好的,我已經制定了如何添加頁腳,但我只能爲每個組添加相同的頁腳。如何在循環中添加頁腳?我的代碼是:OlvColumn1.GroupFormatter = Sub(group as OLVGroup,parms As GroupingParameters) group.Footer = tempDT.Rows(x)(「body」)。ToString End Sub – Riples

+0

你應該改進/編輯你的問題或問一個問題新的一個。 –

回答

0

您可以分析代碼爲ObjectListView類的

public void MakeGroupies<T>(T[] values, string[] descriptions, object[] images, string[] subtitles, string[] tasks) 

method。明確設置 GroupKeyGetter, GroupKeyToTitleConverter GroupFormatter屬性代表。

這是C#,但您的VB適應應該是直截了當的。我使用這個小測試類作爲綁定到列表視圖的對象類型。

public class TestClass 
{ 
    private readonly string _s; 
    private readonly float _f; 

    public TestClass(string p1, float p2) 
    { 
     this._s = p1; 
     this._f = p2; 
    } 

    [OLVColumn(DisplayIndex = 1, Name="S", Title="String")] 
    public string S {get {return this._s;}} 
    [OLVColumn(DisplayIndex = 2, Name = "F", Title = "Float")] 
    public float F {get {return this._f;}} 
} 

以免手動定義我使用的綁定對象中的屬性列性狀和

BrightIdeasSoftware.Generator.GenerateColumns(this.olv, typeof(TestClass)); 

呼叫在我使用的列表視圖的形式/用戶控制。其實這裏是完全隔離ObjectListView配置方法:

 void SetData(TestClass[] objects) 
     { 
      // build list columns 
      Generator.GenerateColumns(this.olv, typeof(TestClass)); 
      // use groups and make current column the priimary sort column 
      this.olv.ShowGroups = true; 
      this.olv.SortGroupItemsByPrimaryColumn = false; 
      // loop through columns and set properties    
      foreach(OLVColumn col in this.olv.Columns) 
      { 
       col.Groupable = true; 
       col.Sortable = true; 
       if(col.Name == "F") 
       { 
        col.MakeGroupies<float>(new float[] { 10f, 100f, 1000f }, new string[] { "<10", "10-100", "100-1000", ">1000" }); 
       } 
       else if(col.Name == "S") 
       { 
        col.UseInitialLetterForGroup = false; 
        // 
        col.GroupKeyGetter = (obj) => 
        { 
         TestClass tc = (TestClass)obj; 
         switch(char.ToLower(tc.S[0])) 
         { 
          case 'a': 
          case 'e': 
          case 'i': 
          case 'o': 
          case 'u': return true; 
          default: return false; 
         } 
        }; 
        // 
        col.GroupKeyToTitleConverter = (o) => { bool b = (bool)o; return b ? "vowel" : "consonant"; }; 
        // 
        col.GroupFormatter = (/*OLVGroup*/ group, /*GroupingParameters*/ parms) => 
        { 
         string s = string.Format ("{0} {1}", group.GroupId, group.Id); 
         //group.BottomDescription = "BottomDescription: " + s; 
         //group.TopDescription = "TopDescription: " + s; 
         group.Footer = "Footer: " + s;       
        }; 

       } 

      } 
      // 
      this.olv.RebuildColumns(); 
      // 
      this.olv.SetObjects(objects); 
     } 

你肯定會有每組一個不同的腳註。

+0

你好,我知道你已經有一段時間了,因爲你爲我發佈了這個答案,但我冒險嘗試另一種方法來嘗試實現更簡單的解決方案。似乎工作良好,直到我需要添加額外的功能到我的ListView,因此我已經結束了在同一地點 - 但現在有了更多的知識。無論如何,我仍然(不幸)似乎不能讓我的頭在你的代碼周圍,我仍然只爲每個分組項目獲得相同的頁腳。我不確定我要去哪裏錯,但希望你可以把你的例子簡化得簡單一點。 – Riples

+0

好吧,從來沒有想過...試驗和錯誤,一個小小的代碼轉換和一些研究很長的路要走。我的數據填充座標位於數據填充循環內,因此每次都返回最後一個頁腳數據。我會將您的答案標記爲已接受,因爲您的代碼絕對會讓我走上正軌。我使用它與此鏈接[here](http://read.pudn.com/downloads105/sourcecode/windows/control/listview/432156/ObjectListViewDemo/MainForm.cs__.htm)再次感謝! – Riples