如何在DevExpress TreeList控件中获取FocusedNode的X和Y坐标?
答案 0 :(得分: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;
}
答案 1 :(得分: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();
}
}
}