【问题标题】:Update a value in treeListView in objectListView在 objectListView 中更新 treeListView 中的值
【发布时间】:2014-10-03 08:48:36
【问题描述】:

我已经建立了一个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()。

【问题讨论】:

    标签: c# objectlistview treelistview


    【解决方案1】:

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

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

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

    【讨论】:

    • 好的,谢谢我已经使用 refreshObjects() 解决了刷新值问题并更改了底层模型。
    • @azuric:太好了。由于此答案有助于解决您的问题,因此您可以接受。谢谢。
    • 我还想知道如何点击一个单元格来触发一个事件?我不想编辑单元格,只是触发一个事件,该事件给我单元格的位置,例如列和行,以便我可以做一些事情
    • CellClick 事件为您提供了有关所涉及的单元格、行和关联模型对象的大量信息。但是如果你只想看到用户选择了一个新行,你通常会使用SelectedIndexChangedSelectionChanged 事件,并在事件处理程序中检查SelectedObject 属性以获取该行的模型对象(或NULL如果未选择任何内容)。如果您仍然遇到问题,请发布一个新问题,因为它不再有助于此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2015-03-24
    • 2011-03-01
    相关资源
    最近更新 更多