【发布时间】:2014-07-28 15:32:25
【问题描述】:
我一直在尝试为 treeListView 构建模型,但似乎无法获得满足我要求的正确结构。我对 objectListView 很陌生,并且查看了示例和食谱,但不确定如何正确构建我的模型。这是我的模型的简化版本:
我有一个父母,我们称它为“A”。
有 2 列(名称、值)。 “A”将是父级的名称,值可以设置为“1”。
“A”有两个没有名称但都带有值的孩子,“2”代表第一个孩子,“3”代表第二个孩子。树停在这一点上。
所以我们有这样的结构:
Name Value
A 1
2
3
这里是设置 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);
}
}
}
我如何阻止子级具有扩展符号 (+),因为它们不是父级而无法扩展?
【问题讨论】:
-
您是否忘记为列设置 AspectName 或安装 AspectGetter?
-
另外,您应该准确地解释您想要实现的目标。请注意,子对象可以是与父对象不同的类型。您想为父级显示名称和值而您只为子级使用值,这似乎有点令人恼火。也许一个单独的 Child 对象会满足您的需求。
-
什么是 AspectName 和 AspectGetter?
-
我没有意识到孩子可能与父母不同。我将如何构建它。我对如何拥有不同的父母和孩子感到有些困惑。
-
好的,我离我想要的更近了一点:
标签: c# .net objectlistview