How do you get the X,Y of a DevExpress TreeList node?

2.7k views Asked by At

How do you get the X and Y coordinates of the FocusedNode in a DevExpress TreeList control?

2

There are 2 answers

0
James On BEST ANSWER

This is the way I have implemented it:

/// <summary>
/// Get the position and size of a displayed node.
/// </summary>
/// <param name="node">The node to get the bounds for.</param>
/// <param name="cellIndex">The cell within the row to get the bounds for.  
/// Defaults to 0.</param>
/// <returns>Bounds if exists or empty rectangle if not.</returns>
public Rectangle GetNodeBounds(NodeBase node, int cellIndex = 0)
{
    // Check row reference
    RowInfo rowInfo = this.ViewInfo.RowsInfo[node];
    if (rowInfo != null)
    {
        // Get cell info from row based on the cell index parameter provided.
        CellInfo cellInfo = this.ViewInfo.RowsInfo[node].Cells[cellIndex] as CellInfo;
        if (cellInfo != null)
        {
            // Return the bounds of the given cell.
            return cellInfo.Bounds;
        }
    }
    return Rectangle.Empty;
}
0
Mike Coxeter On

It is possible to gather geometry data on cells in a xtraTeeList control by hooking the CustomDrawNodeCell event.

The data however will not be available until the cells have been drawn.

In the example below the geometric details are outputted when the focused Node changes.

using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraTreeList;
using System.Windows.Forms;
using System.Drawing;

namespace StackOverflowExamples {
    public class XtraTreeListCellInformation {
        public void How_To_Get_Geometric_Data_From_XtraTreeList() {

            Form frm = new Form();
            Label status = new Label() { Dock = DockStyle.Bottom };
            TreeList list = new TreeList() { Dock = DockStyle.Fill };

            Dictionary<int/*NodeID*/, Rectangle> geometryInfo = new Dictionary<int, Rectangle>();

            frm.Controls.AddRange( new Control[] { list, status } );
            list.DataSource = new[] { new { Name = "One" }, new { Name = "Two" }, new { Name = "Three" } };

            list.CustomDrawNodeCell += ( object sender, CustomDrawNodeCellEventArgs e ) => {
                if( !geometryInfo.ContainsKey( e.Node.Id ) ) {
                    geometryInfo.Add( e.Node.Id, e.Bounds );
                } else {
                    geometryInfo[e.Node.Id] = e.Bounds;
            }
            };

            list.FocusedNodeChanged += ( object sender, FocusedNodeChangedEventArgs e ) => {
                status.Text = "Unknown";
                if( e.Node != null ) {
                    Rectangle rect = Rectangle.Empty;
                    if( geometryInfo.TryGetValue( e.Node.Id, out rect ) ) {
                        status.Text = rect.ToString();
                    } else {
                    status.Text = "Geometry Data Not Ready";
                    }
                }
            };

            frm.ShowDialog();
        }
    }
}