UG内看到的效果如下图,名称无法修改,可以对第二列数字进行修改,隐藏了第一列,隐藏了表头。
选中节点,右键可出现删除菜单,不选中节点,右键可出现新建菜单(为示范内容,实际不需要)
///
/// 初始化
///
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());
}
}
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());
}
}
///
/// 设置右键树列表菜单显示内容与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());
}
}
///
/// 设置右键树列表菜单实现的功能
///
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());
}
}
///
/// 编辑选项时选择回调,可用于树列表的动态更新
///
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;
}
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;
}