【问题标题】:How do you get the X,Y of a DevExpress TreeList node?如何获得 DevExpress TreeList 节点的 X、Y?
【发布时间】:2012-08-02 15:25:56
【问题描述】:

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

【问题讨论】:

    标签: c# winforms xtratreelist


    【解决方案1】:

    这是我实现它的方式:

    /// <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;
    }
    

    【讨论】:

      【解决方案2】:

      可以通过挂钩 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();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-10
        • 2015-01-09
        • 2012-08-31
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多