1

如何在 DevExpress TreeList 控件中获取 FocusedNode 的 X 和 Y 坐标?

4

2 回答 2

2

这是我实现它的方式:

/// <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;
}
于 2012-09-04T14:25:39.270 回答
0

通过挂钩 CustomDrawNodeCell 事件,可以在 xtraTeeList 控件中的单元格上收集几何数据。

但是,在绘制单元格之前,数据将不可用。

在下面的示例中,当焦点节点发生变化时会输出几何细节。

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();
        }
    }
}
于 2012-08-14T13:58:21.720 回答