Windows Phone Developers

Tuesday, February 17, 2009

How to get the selected node in tree view from MouseDown event using C#

C# - MouseDown event in Tree View Control

Here is a way to get the selected tree view node using C#. The snippet uses the X and Y co-ordinates to get the node

private void treeView1_MouseDown(object sender, MouseEventArgs e)

{

Point p1 = new Point(e.X, e.Y);

TreeNode N1 = treeView1.GetNodeAt(p1) ;

MessageBox.Show(N1.Text);

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

3 comments:

  1. Who ever posted this solution never tried it in a NET application because it doesn't work

    ReplyDelete
  2. Same problem
    This solution works only on WPC. It doesn't work on a .NET application

    ReplyDelete
  3. Hi,
    It does work in .NET as well. What I did was creating a custom treeView, and override the OnMouseDown method:

    class CustomTreeView : TreeView
    {
    protected override void OnMouseDown(MouseEventArgs e)
    {
    base.OnMouseDown(e);
    this.SelectedNode = this.GetNodeAt(e.Location);
    }
    }

    I hope to help!

    ReplyDelete