Winform中双击DevExpress的TreeList的树形节点怎样获取当前节点

场景

DevExpress的TreeList怎样设置数据源,从实例入手:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102548490

鼠标双击TreeList中的某一节点,在双击事件中怎样获取当前节点。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

双击事件绑定

treeList.MouseDoubleClick += treeList_MouseDoubleClick_InOne;

双击事件中

将sender作为treeList,再将treeList的数据源取出,是一个节点的List。

然后获取当前选中的树形节点的Id属性

通过linq表达式获取与Id想对应的节点,进而获取节点对象。

示例代码:

private static void treeList_MouseDoubleClick_InOne(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                DevExpress.XtraTreeList.TreeList treeList = sender as DevExpress.XtraTreeList.TreeList;
                if (treeList != null && treeList.Selection.Count == 1)
                {
                    object idValue = null;
                    string strIdValue = String.Empty;
                    DataTreeNode nodeData = null;
                    List datasource = treeList.DataSource as List;
                    if (datasource != null)
                    {
                        idValue = treeList.Selection[0].GetValue("Id");
                        strIdValue = idValue.ToString();
                        nodeData = datasource.Where(p => p.Id == strIdValue).FirstOrDefault();
                        if (nodeData != null)
                        {
                           
                        }
                    }
                }
            }
        }

其中DataTreeNode参考上面博客。

你可能感兴趣的:(C#,treeList)