我通过下一个方法填充我的项目中的treeview:
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的值?
答案 0 :(得分:2)
您是否考虑过使用SetValues()函数? 下面是一个示例,它将仅使用您想要的值填充选定的原始数据。 您可以轻松修改此代码,以便可以使用TreePath将值更改为任何特定行:
TreeIter iter; TreeModel model;
if( MyTreeView.Selection.GetSelected(out model, out iter))
TreeViewListStore.SetValues(iter,"column1_1","column1_2","column1_3");
答案 1 :(得分: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");
}
}