locked
WPF TreeView NodeMouseDoubleClick display data RRS feed

  • Question

  • I want to display data when click on the node, but how to detect which node is clicked from which row? for gridview we can indicate by using e.row. how about tree view, i use .Notes why will not work ( thank for helping)

     private void tv_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                if (e.Node.Nodes.Count == 0)
                {
      RecordToEdit(GetSelectedRecord(e.Nodes));
                }
            }
    
    
    private ABCGetSet GetSelectedRecord(int RowIndex)
            {
                TreeNode rowSelected = tv.Nodes[RowIndex].Index TreeNode;
    
                return Utilities.PopulateDataElementByDataRow<ABCGetSet>(rowSelected.Nodes);
            }

    Thursday, November 20, 2014 9:37 AM

Answers

  • There is no row index in a TreeView since it is hierarchical...but you could (should!) pass the clicked TreeNode to the GetSelectedRecord method:

      
      void tv_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
          TreeNode clickedNode = e.Node;
          GetSelectedRecord(clickedNode);
        }
    
    
        private ABCGetSet GetSelectedRecord(TreeNode rowSelected)
            {
                return Utilities.PopulateDataElementByDataRow<ABCGetSet>(rowSelected.Nodes);
            }
    


    Once again, please remember to mark helpful posts as answer and/or helpful.

     

    Thursday, November 20, 2014 12:58 PM

All replies

  • I think your query is about WinForms treeview not about WPF treeView. I cant understand your query. 

    First of all your code will throw error, TreeViewMouseClickEventArgs wont have any thing as Nodes. If you want the nodes, you can directly take if from e.Node.Nodes.

    Why you want the row/index?

    To take the level and index/row of a node.

    private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        int level = e.Node.Level;
        int row = e.Node.Index;
    }


    sri

    • Proposed as answer by JayChase Thursday, November 20, 2014 11:52 AM
    Thursday, November 20, 2014 10:52 AM
  • I would use mvvm and a probably a behaviour myself.

    I think with events:

    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <EventSetter Event="MouseDoubleClick" Handler="ItemDoubleClick" />
    You can cast the sender to a treeViewItem
       private void ItemDoubleClick(object sender, MouseButtonEventArgs args)
        {
            if (sender is TreeViewItem)
            {
                if (!((TreeViewItem)sender).IsSelected)
                {
                    return;
                }
            }
    
         // Do stuff with treeviewitem


    • Edited by Andy ONeill Thursday, November 20, 2014 10:56 AM
    Thursday, November 20, 2014 10:55 AM
  • The e.Node property returns the clicked node:

      public partial class Form1 : Form
      {
        public Form1() {
          InitializeComponent();
    
    
          treeView1.BeginUpdate();
          treeView1.Nodes.Add("Parent");
          treeView1.Nodes[0].Nodes.Add("Child 1");
          treeView1.Nodes[0].Nodes.Add("Child 2");
          treeView1.Nodes[0].Nodes[1].Nodes.Add("Grandchild");
          treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Great Grandchild");
          treeView1.EndUpdate();
    
          treeView1.NodeMouseDoubleClick += treeView1_NodeMouseDoubleClick;
    
        }
    
        void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
          TreeNode clickedNode = e.Node;
          MessageBox.Show("You clicked on " + clickedNode.Text);
        }
    
    
      }
    

    Please remember to mark helpful posts as answer and/or helpful.

    Thursday, November 20, 2014 11:15 AM
  • private ABCGetSet GetSelectedRecord(int RowIndex)
            {
                TreeNode rowSelected = tv.Nodes[RowIndex].Index TreeNode;
    
                return Utilities.PopulateDataElementByDataRow<ABCGetSet>(rowSelected.Nodes);
            }

    below code how to pass in the selected row?

    TreeNode rowSelected = tvProductSubCategoryList.SelectedNode[selectedRowIndex].Nodes as TreeNode;

    Thursday, November 20, 2014 12:22 PM
  • There is no row index in a TreeView since it is hierarchical...but you could (should!) pass the clicked TreeNode to the GetSelectedRecord method:

      
      void tv_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
          TreeNode clickedNode = e.Node;
          GetSelectedRecord(clickedNode);
        }
    
    
        private ABCGetSet GetSelectedRecord(TreeNode rowSelected)
            {
                return Utilities.PopulateDataElementByDataRow<ABCGetSet>(rowSelected.Nodes);
            }
    


    Once again, please remember to mark helpful posts as answer and/or helpful.

     

    Thursday, November 20, 2014 12:58 PM
  • currently all the geter and satter is at abc.cs, when i check on node, with pass in e.node.index that is not enough

    private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                PopulateRecordToEdit(GetSelectedRecord(e.Node.Index));
            }


    Friday, November 21, 2014 4:46 AM