I searched for hours but either my programming approach is wrong or I didn't have the correct searching ideas...
Lot of time ago, I discovered ObjectListView as an nice alternative to normal ListView. My Problem in my current Project:
I didn't have an static model class. I have a lot of objects of different kinds. Each kind of object has different attributes. In my ObjectListView I want to show the Name of the Object and its attributes, a row for each object of given kind. (For one kind of a Object, the attributes are the same)
For my Model of ObjectListView I tried following Mini-example (using "num" and "txt" as AspactName):
using System.Dynamic;
[...]
private void addItems()
{
dynamic expando = new ExpandoObject();
expando.num = 1;
expando.txt = "Element";
List<ExpandoObject> lst = new List<ExpandoObject>();
lst.Add(expando);
olv.SetObjects(lst);
}
ObjectListView adds an Row but doesn't show up the text of the Properties num and txt.
Any Idea to solve this problem? Is there another approach to show elements of a List (like Object.Attributes) as Columns using ObjectListView?
Thank you in advance.
Okay, Thank you Klaus, following your idea in my mind and searching again for special Objects and ObjectListView pointed me to AspectGetterDelegates
I found a solution working for the first time: adding following AspectGetter Delegate, the ObjectListView shows the information of my dynamicaly generated Properties:
olv.GetColumn(0).AspectGetter = delegate (dynamic er) { return er.Nummer; };
olv.GetColumn(1).AspectGetter = delegate (dynamic er) { return er.Text; };
If someone has the same Problem, here are two useful Methods for Setting and Getting Properties from ExpandoObjects:
public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
{
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(propertyName))
expandoDict[propertyName] = propertyValue;
else
expandoDict.Add(propertyName, propertyValue);
}
public static object GetProperty(ExpandoObject expando, string propertyName)
{
var expandoDict = expando as IDictionary<string, object>;
if (expandoDict.ContainsKey(propertyName))
return expandoDict[propertyName];
else
return null;
}