1

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?

4

2 回答 2

5

在手动绘制子项(使用DrawSubItem事件)的过程中,我无意中发现,简单地将ObjectListView属性设置OwnerDraw为true,实际上是使图像居中。

为了清楚起见,以下是仅包含图像的列的绘制方式:

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"; };

ObjectListViewSmallImageList设置为使用我为它创建的 ImageList,这是该ImageGetter方法使用图像名称(本例中为“图标”)的地方。

默认情况下,无论文本对齐等设置如何,图像都绘制在列的最左侧。

通过将OwnerDrawObjectListView 设置为 true,图像奇迹般地在列中居中,无需添加任何其他代码。这是出乎意料的行为,可能值得更多调查。现在它有效,所以也许这个技巧也可以帮助其他人。

于 2012-02-09T22:32:00.007 回答
0

在绘制图像时如何使列表所有者绘制并居中(计算 x 知道列边界)。现在正在寻找一个例子..

此处:ListView 中的中心子项图标

希望能帮助到你。

于 2012-02-02T15:20:44.073 回答