Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在objectListView中为treeListView构建模型_C#_.net_Objectlistview - Fatal编程技术网

C# 在objectListView中为treeListView构建模型

C# 在objectListView中为treeListView构建模型,c#,.net,objectlistview,C#,.net,Objectlistview,我一直在尝试为treeListView构建一个模型,但似乎无法获得适合我需求的正确结构。我对objectListView非常陌生,并且已经看过了示例和食谱,但不确定如何正确构建我的模型。以下是我的模型的简化版本: 我有一个父母,让我们称之为“a” 共有2列(名称、值)。“A”将是父级的名称,值可以设置为“1” “A”有两个子项,它们没有名字,但都带有值,“2”表示第一个子项,“3”表示第二个子项。这棵树停在这一点上 我们有这样一个结构: Name Value A 1

我一直在尝试为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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间TreeListViewTest1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
this.treeListView1.CanExpandGetter=委托(对象x)
{ 
返回true;
};
this.treeListView1.ChildrenGetter=委托(对象x)
{
合同=x为合同;
返回合同儿童;
};
column1.AspectGetter=委托(对象x)
{
如果(x是合同)
{
退货((合同)x).名称;
}
其他的
{
返回“”;
}
};
column2.AspectGetter=委托(对象x)
{
如果(x是合同)
{
退货((合同)x).价值;
}
其他的
{
双d=(双)x;
返回d.ToString();
}
};
this.treeListView1.AddObject(新合同(“A”,1));
}
私有无效树列表视图1\u SelectedIndexChanged(对象发送方,事件参数e)
{
}
}
公共类合同
{
公共字符串名称{get;set;}
公共双值{get;set;}
公共列表子项{get;set;}
公共合同(字符串名称,双值)
{
名称=名称;
价值=价值;
Children=新列表();
增加(2);
增加(3);
}
}
}
我如何阻止孩子使用扩展符号(+),因为他们不是家长,所以无法扩展


什么是AspectName和AspectGetter? 您必须告诉列,从哪里获取数据。可以将列的AspectName属性设置为model objects属性的名称,也可以使用AspectGetter委托来完全控制列中的内容

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 "";
    }
};

我没有意识到孩子和父母是不同的。我将如何构建它。我有点困惑,你怎么会有不同的父母和孩子

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

我如何阻止孩子使用扩展符号(+),因为他们不是家长,所以无法扩展

您必须检查对象是否确实有子对象。如果是这种情况,则仅在CanExpandGetter中返回
true
。例如:

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

您是否忘记为列设置AspectName或安装AspectGetter?此外,您还应该准确地解释您想要实现的目标。请注意,子对象可以是与父对象不同的类型。似乎有点恼人的是,您希望为父对象显示名称和值,而只为子对象使用值。也许一个单独的子对象可以满足您的需要。AspectName和AspectGetter是什么?我不知道子对象可以与父对象不同。我将如何构建它。我有点困惑,你如何能有一个不同的父母和孩子。好吧,我越来越接近我想要的:完美,请看我上面更新的代码。我有一个关于如何阻止孩子们拥有可扩展选项(+)的问题?因为这会导致错误。我还在另一篇帖子中使用了你的答案: