【C#】NXOpen树列表的操作

UG内看到的效果如下图,名称无法修改,可以对第二列数字进行修改,隐藏了第一列,隐藏了表头。

选中节点,右键可出现删除菜单,不选中节点,右键可出现新建菜单(为示范内容,实际不需要)
【C#】NXOpen树列表的操作_第1张图片

  1. 首先需要在初始化内设置,这里主要设置了树列表内的鼠标右键菜单,与节点内容修改时的动作回调,以及设置了树列表高度
        /// 
        /// 初始化
        /// 
        public void initialize_cb()
        {
     
            try
            {
     
                group0 = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group0");
                tree_control0 = (NXOpen.BlockStyler.Tree)theDialog.TopBlock.FindBlock("tree_control0");
                
                //------------------------------------------------------------------------------
                //MenuHandler
                //------------------------------------------------------------------------------
                tree_control0.SetOnMenuHandler(new NXOpen.BlockStyler.Tree.OnMenuCallback(OnMenuCallback));
                tree_control0.SetOnMenuSelectionHandler(new NXOpen.BlockStyler.Tree.OnMenuSelectionCallback(OnMenuSelectionCallback));
                tree_control0.SetOnEditOptionSelectedHandler(new NXOpen.BlockStyler.Tree.OnEditOptionSelectedCallback(OnEditOptionSelectedCallback));
                tree_control0.SetAskEditControlHandler(new NXOpen.BlockStyler.Tree.AskEditControlCallback(AskEditControlCallback));

                tree_control0.Height = 200;
            }
            catch (Exception ex)
            {
     
                theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
            }
        }
  1. dialogShown_cb内设置树列表的布局,插入列,设置列宽,设置列标题是否显示
        public void dialogShown_cb()
        {
     
            try
            {
     
                tree_control0.ShowHeader = false;
                tree_control0.ShowExpandCollapseMarker = true;
                // Insert Columns插入列
                tree_control0.InsertColumn( 0, "Name",0);
                tree_control0.InsertColumn( 1, "LocalName",120);
                tree_control0.InsertColumn( 2, "Layer", 120);

                // Set resize policy for columns 设置列随大小调整
                tree_control0.SetColumnResizePolicy( 0, Tree.ColumnResizePolicy.ConstantWidth);
                tree_control0.SetColumnResizePolicy( 1, Tree.ColumnResizePolicy.ConstantWidth);
                tree_control0.SetColumnResizePolicy( 2, Tree.ColumnResizePolicy.ResizeWithTree);

                LoadDataInTreeList(); //载入xml文件
            }
            catch (Exception ex)
            {
     
                //---- Enter your exception handling code here -----
                theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
            }
        }
  1. 用于设置右键树列表菜单显示内容与ID,当选中节点时,显示删除节点,没有选中任何节点时,显示添加节点。
    /// 
    /// 设置右键树列表菜单显示内容与ID
    /// 
    public void OnMenuCallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int columnID)
    {
     
        try
        {
     
            //创建菜单
            TreeListMenu menu = tree.CreateMenu();
            //如果没有选中节点,则显示新建
            if (node == null)
            {
     
                menu.AddMenuItem(1, "新建");
                menu.SetItemIcon(1,"ico");
            }
            else
            {
     
                menu.AddMenuItem(0, "删除");
                menu.SetItemIcon(0, "delete");
                menu.SetItemDisable(0, false);
            }

            //设置菜单
            tree.SetMenu(menu);
        }
        catch (Exception ex)
        {
     
            theUI.NXMessageBox.Show("Block Styler", NXOpen.NXMessageBox.DialogType.Error, ex.ToString());
        }
    }
  1. 设置右键树列表菜单实现的功能,与上方定义的菜单ID对应。
    /// 
    /// 设置右键树列表菜单实现的功能
    /// 
    public void OnMenuSelectionCallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int menuItemID)
    {
     
        try
        {
     
        	//对应上面设置的菜单ID
            if (menuItemID == 0)
            {
     
                 DeleteSelectedNodes();//删除选中节点
            }
            else if (menuItemID == 1)
            {
     
                AddNode();//添加节点
            }

        }
        catch (Exception ex)
        {
     
            theUI.NXMessageBox.Show("Block Styler", NXOpen.NXMessageBox.DialogType.Error, ex.ToString());
        }
    }
  1. 开启编辑控件选项,如果返回的为 Tree.EditControlOption.Reject; 则无法编辑树列表节点
        /// 
        /// 编辑选项时选择回调,可用于树列表的动态更新
        /// 
        public Tree.EditControlOption OnEditOptionSelectedCallback(NXOpen.BlockStyler.Tree tree, 
        NXOpen.BlockStyler.Node node, int columnID, int selectedOptionID, 
        string selectedOptionText, Tree.ControlType type)
        {
     
            return Tree.EditControlOption.Accept;
        }
  1. 设置第二列可以进行输入与下拉修改数值,其他列不可修改
    public Tree.ControlType AskEditControlCallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int columnID)
    {
     
            if (columnID == 2)//设置图层节点可进行下拉修改
            {
     
                string[] options = {
      node.GetColumnDisplayText(columnID) };//获取当前显示的字符串到数组中
                tree.SetEditOptions(options, 0);//设置默认显示内容为获取的字符串
                return Tree.ControlType.ComboBox;
            }
            return Tree.ControlType.None;
    }

你可能感兴趣的:(学习心得,c#)