2017-02-21 77 views
1

使用渲染和FormatRow使用ObjectListView控制時,我發現一個問題:http://objectlistview.sourceforge.net/cs/index.html在ObjectListView

我想用FormatRow設置自定義字體和每行的顏色屬性。 我還想使用自定義渲染器來插入例如。用戶控件在其中一列中。 但是,這兩者似乎有衝突。

代碼:

//create some bogus list 
List<MyObject> list = new List<MyObject>(); 
list.Add(new MyObject("Test11", "Test12", "Test13")); 
list.Add(new MyObject("Test21", "Test22", "Test23")); 
list.Add(new MyObject("Test31", "Test32", "Test33")); 

//make the 2nd row red 
listView1.FormatRow += delegate (object sender1, BrightIdeasSoftware.FormatRowEventArgs e1) { 
    MyObject x = (MyObject)e1.Model; 
    if (x.Col1 == "Test21") 
    { 
     e1.Item.BackColor = Color.Red; 
    } 
}; 

//add a custom renderer to col2. This will enable us to insert eg. user controls or do other crazy stuff. 
//when we add this, the FormatRow above wont't work. Even though it isn't in effect. Eg. return false. 
olvColumn2.RendererDelegate = delegate (EventArgs e1, Graphics g, Rectangle r, object x) 
{ 
    return false; 
}; 

//set list 
listView1.SetObjects(list); 

我還創建了一個小樣本project

有誰知道解決的辦法?

回答

1

我覺得我找到了解決方案。您應該使用Renderer類,而不是使用RendererDelegate。例如。

private class MyColumnRenderer : BrightIdeasSoftware.BaseRenderer 
{ 
     public override bool RenderSubItem(DrawListViewSubItemEventArgs e, Graphics g, Rectangle cellBounds, object x) 
     { 
      //do you own stuff here 

      //default rendering 
      return base.RenderSubItem(e, g, cellBounds, x); 
     } 
} 

//... 

olvColumn2.Renderer = new MyColumnRenderer();