gpt4 book ai didi

c# - 在 objectListView 中为 treeListView 构建模型

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:00 24 4
gpt4 key购买 nike

我一直在尝试为 treeListView 构建模型,但似乎无法获得满足我要求的正确结构。我对 objectListView 还很陌生,已经浏览了样本和说明书,但不确定如何正确构建我的模型。这是我的模型的简化版本:

我有一个 parent 让我们称它为“A”。

有 2 列(名称、值)。 “A”将是父项的名称,值可以设置为“1”。

“A”有两个没有名称但都带有值的 child ,第一个 child 为“2”,第二个 child 为“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);
}
}
}

我如何阻止子项具有展开符号 (+),因为它们不是父项而无法展开?

最佳答案

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 示例

您可以在 VS 设计器中设置方面名称,也可以像这样手动设置:

// this would tell the column to get the content from the property named "Name"
olvColumn1.AspectName = "Name";

AspectGetter 示例

我们在此示例中附加了一个匿名方法,您也可以使用具有匹配签名的方法名称。

// 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.

只需在 ChildrenGetter 中返回任何类型的列表。使用 AspectName,ObjectListView 只是尝试查找具有给定名称的属性。使用 Aspectgetter,您无论如何都可以手动处理内容,并且可以从行中检查模型的类型。

How do I stop the Children from having an expansion symbol (+) since they cannot be expanded because they are not parents?

您必须检查该对象是否真的有任何子对象。如果是这种情况,仅在 CanExpandGetter 中返回 true。示例:

this.treeListView1.CanExpandGetter = delegate(object x) { 
if (rowObject is MyObject) {
return (((MyObject)x).Children.Count > 0);
} else {
return false;
}
};

关于c# - 在 objectListView 中为 treeListView 构建模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24998757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com