I am using ObjectListView in a C# winforms project and want to show a small icon in one of the columns.
So far, the icon is showing just fine, but it isn't centered in the column, and I cannot find any documentation or code that pertains to image alignment within a column.
Here are the instructions for displaying an image as the column contents.
The code is virtually the same as the example given:
col_Important.AspectGetter = delegate(object x) { return ((ClassMyItem)x).IsImportant; };
col_Important.AspectToStringConverter = delegate(object x) { return String.Empty; };
col_Important.ImageGetter = delegate(object x)
{
return ((ClassMyItem)x).IsImportant? "star" : "none";
};
Has anyone dealt with this issue and know of a way to center the image?
In the process of manually drawing the subitem (using the DrawSubItem
event), I inadvertently discovered that simply setting the ObjectListView property OwnerDraw
to true actually centers the image.
Just to be clear, here is how the columns with only an image are drawn:
colName.AspectGetter = delegate(object x) { return ((MyClass)x).SomeProperty; };
colName.AspectToStringConverter = delegate(object x) { return String.Empty }; };
colName.ImageGetter =
delegate(object x) { return ((MyClass)x).SomeProperty ? "icon" : "none"; };
The ObjectListView has its SmallImageList
set to use the ImageList I created for it, which is where the ImageGetter
method makes use of the image name ("icon" in this example).
By default, the images are drawn at the far left of the column, regardless of settings like text alignment, etc.
By setting OwnerDraw
to true for the ObjectListView, the images are miraculously centered in the column with no other code addition whatsoever. This was unexpected behavior which probably merits more investigation. For now it works, so maybe this tip will help someone else as well.