1

我建立了一个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);
        }
    }
}

它给出了这个输出:

Name     Value

A        1

         2

         3

如何从事件中更新父项和子项的 column2(“值”)值?(增加父母和孩子的每个价值。)

private void button1_Click(object sender, EventArgs e)
{

}

我不明白我是否必须再次使用 AspectGetter,或者我是否可以以某种方式只修改 column2 中的值,然后再 refreshObjects()。

4

1 回答 1

3

我不明白我是否必须再次使用 AspectGetter,或者我是否可以以某种方式只修改 column2 中的值,然后再 refreshObjects()。

不,您应该只操作底层模型对象并调用treeListView1.RefreshObject(myObject);例如,在您的情况下myObject应该在哪里Contract。这将刷新相应行的内容。

我不知道您的 button1_Click() 应该做什么,但是您显然需要引用要刷新的对象。这可能是当前选定的对象,例如 ( treeListView.SelectedObject)。如果你说出你想做什么,我也许可以给你更多的信息。

于 2014-08-19T11:42:28.827 回答