如何将选定的行从TreeView传输到另一个TreeView? GTK#

时间:2018-05-23 13:25:45

标签: c# mono monodevelop gtk# gtktreeview

点击selected rows后如何将TreeviewTreeview转移到另一个Button

我一直在尝试使用ListStore选项创建treeview1并将其作为treeview2提交给TreeModel,但我没有&#39}。成功了。

更新我找到了一种方法,使用OnTreeservicesRowActivated OnTreeservicesRowActivated事件 (仍然可以通过按钮完成) 和下面的代码。它"混合"如果我点击太快,行,但这是一个小麻烦。谢谢你的回答

using System;
using Gtk;
using MySql.Data.MySqlClient;

namespace Proyect
{
   public partial class Window : Gtk.Window
{
       public Window() :
            base(Gtk.WindowType.Toplevel)
        {
            this.Build();
            //Adds columns to TreeViews
            CreateTreeView(treeservices,tree2);
            treeservices.Selection.Mode = SelectionMode.Single;
        }

//List to populate tree2.
ListStore tree2list = new ListStore(typeof(string), typeof(string), typeof(string));

//Class for validating entry's and misc.
    Validation val = new Validation();

//MySql connection
    MySqlConnection c = new MySqlConnection(//connectionstring)
    MySqlDataReader dr;
    MySqlCommand cmd;

    //CellRendererText for treeservices.
    CellRendererText cellserv1 = new CellRendererText();
    CellRendererText cellserv2 = new CellRendererText();
    CellRendererText cellserv3 = new CellRendererText();


    void CreateTreeView(TreeView treeservices, TreeView tree2 )
    {
        treeservices.AppendColumn("Code", cellserv1, "text", 0);
        treeservices.AppendColumn("Name", cellserv2, "text", 1);
        treeservices.AppendColumn("Price", cellserv3, "text", 2);
        treeservices.Model = Services();

        tree2.AppendColumn("Code", new CellRendererText(), "text", 0);
        tree2.AppendColumn("Name", new CellRendererText(), "text", 1);
        tree2.AppendColumn("Price", new CellRendererText(), "text", 2);
    }


    ListStore Services()
    {
        //Returns a ListStore from local MySql Database
    }

//Adds a service to tree2 when a row is double clicked
    protected void OnTreeservicesRowActivated(object o, RowActivatedArgs args)
    {   
        tree2list.AppendValues(cellserv1.Text, cellserv2.Text, cellserv3.Text);
        tree2.Model = tree2list;
    }
}

1 个答案:

答案 0 :(得分:1)

在树视图对象上添加Afterselect事件

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
      //Set TreeNode variable to the selected node
      TreeNode Nodetosave = this.treeView1.SelectedNode;
      //Add Nodetosave to your second tree in your button event
    }

这是我的第一个stackoverflow答案,请原谅我是否错过任何格式化。如果要存储多个TreeNode,则创建一个TreeNode列表并添加每个选定的treenode,然后在按钮事件中迭代它们并将它们逐个添加到另一个树中。