【问题标题】:Building a model for treeListView in objectListView在 objectListView 中为 treeListView 构建模型
【发布时间】: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


【解决方案1】:

什么是 AspectName 和 AspectGetter? 您必须告诉列,从哪里获取数据。您可以将列的 AspectName 属性设置为模型对象属性的名称,也可以使用 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 中返回任何类型的 List。使用 AspectName,ObjectListView 只是尝试查找具有给定名称的属性。使用 Aspectgetter,无论如何您都可以手动处理内容,并且可以从行中检查模型的类型。

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

你必须检查对象是否真的有任何孩子。如果是这样,请仅在 CanExpandGetter 中返回 true。示例:

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

【讨论】:

  • 完美地看到我上面的更新代码。我有一个关于如何阻止孩子拥有可扩展选项(+)的问题?因为这会导致错误。我也在另一篇文章中使用了你的答案:stackoverflow.com/questions/19188245/…
猜你喜欢
  • 2013-01-31
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
相关资源
最近更新 更多