html5显示数组listview,ObjectListView-显示数组

Suppose I have this code:

public sealed class MyStruct {

// ... snip

public uint[] ItemStatValue { get; set; }

// ... snip

}

// MainForm.cs

// ...

Generator.GenerateColumns(this.ContentListView, structure, true);

ContentListView.SetObjects(_records);

// ...

Is there a way to instruct GenerateColumns to treat each element of the ItemStateValue property as a column on its own, and appropriately name them ? ("Item Stat Value 1", "Item Stat Value 2", etc) Currently, it just calls ToString() on the object, thus returning System.Type.UInt32[]. Not exactly what I'd want.

Cheers!

Solutions1

No, that is not possible. The OLV uses reflection on the type of the specified item to gather the required information. If you supply an IEnumerable it just looks at the type of the first item.

The Generator has no clue and does not care about the actual instances of your items. Also, it wouldn't make much sense in your proposed case, since the number of elements could differ between each struct.

Solutions2

How many items are going to be in the array? ListViews have a fixed number of columns, i.e. each row has the same number of columns.

If you know there is going to be, say, 20 possible stats, just generate the columns.

const int numberOfColumns = 20;

for (int i = 0; i < numberOfColumns; i++) {

var statIndex = i;

var column = new OLVColumn();

column.Name = "Stat" + i;

column.AspectGetter = delegate(object x) {

MyStruct myStruct = (MyStruct)x;

return statIndex < myStruct.ItemStatValue.Length ? (uint?)myStruct.ItemStatValue[statIndex] : null;

};

column.AspectToStringConverter = delegate(object x) {

uint? value = (uint?)x;

return value.HasValue ? String.Format("Stat value: {0}", value.Value) : String.Empty;

};

this.ContentListView.AllColumns.Add(column);

}

this.ContentListView.RebuildColumns();

Call this after the GenerateColumns()

Talk1:

I (sadly) had to add some code to Generator.GenerateColumns. I'll paste it below. Thanks, though!

Solutions3

@Grammarian

So after looking around in the code, I noticed aspect getters for fields. However, Generator.GenerateColumns didn't seem to use its third boolean parameter, named allProperties.

So I threw together this quick code, and sure enough, it worked. It doesn't seem to cause any bug either, which is great.

Here is a gist:

Note: this also requires that you allow OLVColumnAttribute and OLVIgnoreAttribute to be applied to fields. That's easy enough ;)

Cheers!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值