I have been trying to build a model for treeListView but cannot seem to get the right structure for my requirements. I am quite new to objectListView and have looked through the samples and cookbook but not sure how build my model correctly. Here is simplified version of my model:
I have a parent lets call it "A".
There are 2 columns (Name, Value). "A" would be the Name of the parent and Value can be set to "1".
"A" has two children which do not have a Name but both carry a Value, "2" for the 1st child and "3" for the second child. The tree stops at this point.
So we have a structure like so:
Name Value
A 1
2
3
Here is the code to setup the treeListView:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TreeListViewTest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.treeListView1.CanExpandGetter = delegate(object x)
{
return true;
};
this.treeListView1.ChildrenGetter = delegate(object x)
{
Contract contract = x as Contract;
return contrat.Children;
};
column1.AspectGetter = delegate(object x)
{
if(x is Contract)
{
return ((Contract)x).Name;
}
else
{
return " ";
}
};
column2.AspectGetter = delegate(object x)
{
if(x is Contract)
{
return ((Contract)x).Value;
}
else
{
Double d = (Double)x;
return d.ToString();
}
};
this.treeListView1.AddObject(new Contract("A", 1));
}
private void treeListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
public class Contract
{
public string Name { get; set;}
public Double Value { get; set; }
public List<Double> Children {get; set;}
public Contract(string name, Double value)
{
Name = name;
Value = value;
Children = new List<Double>();
Children.Add(2);
Children.Add(3);
}
}
}
How do I stop the Children from having an expansion symbol (+) since they cannot be expanded because they are not parents?
What is AspectName and AspectGetter? You have to tell the Columns, where to get the data from. You can either set the AspectName property of a column to the name of the model objects property OR you can use an AspectGetter delegate that gives you complete control about what to put in the column.
AspectName example
You can set the aspect name from within the VS designer or set it manually like this:
// this would tell the column to get the content from the property named "Name"
olvColumn1.AspectName = "Name";
AspectGetter example
We attach an anonymous method in this example, you could also use a method name with matching signature.
// this lets you handle the model object directly
olvColumn1.AspectGetter = delegate(object rowObject) {
// check if that is the expected model type
if (rowObject is MyObject) {
// just return the value of "Name" in this simple case
return ((MyObject)rowObject).Name;
} else {
return "";
}
};
I didn't realise the child can be different from the parent. How would i construct that. I am a little confused as to how you can have a different parent and child.
Just return any kind of List in the ChildrenGetter. Using the AspectName, the ObjectListView just tries to find a property with the given name. Using the Aspectgetter, you handle the contents manually anyway and can check the type of the model from the row.
How do I stop the Children from having an expansion symbol (+) since they cannot be expanded because they are not parents?
You have to check if the object actually has any children. Only return true
in the CanExpandGetter if thats the case. Example:
this.treeListView1.CanExpandGetter = delegate(object x) {
if (rowObject is MyObject) {
return (((MyObject)x).Children.Count > 0);
} else {
return false;
}
};