1

我通过下一个方法在我的项目中填充树视图:

            Gtk.ListStore treeview1ListStore = new Gtk.ListStore (typeof (String), typeof (String));
protected void FillTreeView(TreeView MyTreeView, Gtk.ListStore TreeViewListStore, String[] Column1, String[] Column2)
{
    for(Int32 i=0;i!=Column1.Length;i++)
    {
       TreeViewListStore.AppendValues(Column1[i],Column2[i]);
    }
    MyTreeView.Model=TreeViewListStore
}

我通过以下方式调用此方法:

String[] column1 = new String[]{"column1_1","column1_2","column1_3"};
String[] column2 = new String[]{"column2_1","column2_2","column2_3"};
this.FillTreeView(this.treeview1, treeview1ListStore, column1,column2);

现在我想更改一些树视图值,但是当我使用时:

    foreach(Gtk.Object currentNode in treeviewListStore)
    {

    }

我的程序崩溃了。如何更改 ListStore 的值?

4

2 回答 2

2

你有没有想过使用 SetValues() 函数?这是一个示例,它将仅用您想要的值填充选定的原始数据。您可以轻松修改此代码,以便使用 TreePath 将值更改为任何特定行:

TreeIter iter; TreeModel model;

if( MyTreeView.Selection.GetSelected(out model, out iter))
    TreeViewListStore.SetValues(iter,"column1_1","column1_2","column1_3");
于 2012-11-25T15:53:53.840 回答
0

这是一个在特定“行”上进行更新的示例 - columnNumber 用作模型中的索引:

    public void updateDataOfListStoreInTreeView(ListStore mListstore, int ColumnNumber, params string[] str)
    {
        try {
            bool bUpdated = false;
            TreeIter tmpTreeIter; 

            mListStore.GetIterFirst(out tmpTreeIter);
            object o = mListStore.GetValue(tmpTreeIter, ColumnNumber);
            while(o!=null)
            {
                if(o.ToString()==str[ColumnNumber].ToString()) {
                    mListStore.SetValues(tmpTreeIter,str); // update row
                    bUpdated=true;
                    break;
                }
                if(mListStore.IterNext(ref tmpTreeIter)) {
                    o = mListStore.GetValue(tmpTreeIter, ColumnNumber);
                }
                else 
                    o = null;
            }

            if(!bUpdated)  
                mListStore.AppendValues (str); // Add some data to the store
        }
        catch (Exception e)
        {
            Console.WriteLine("WARNING: adding to treeview caused exception");
        }
    }
于 2016-06-06T12:57:46.227 回答