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

那绪
2023-12-01

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!

 类似资料: