使用ObjectListView控件时发现了一个问题:http://objectlistview.sourceforge.net/cs/index.html
我想使用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
有没有人知道解决这个问题?
答案 0 :(得分: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();