/// <summary> 移除一个节点 /// /// </summary> public void Remove(MindMapNodeContainer MindMapNodeParame) { if (MindMapNodeParame == null) { return; } if (NodeSizeChanged != null) { NodeSizeChanged(); //隐藏思维导图用以跳过绘制过程 } MindMapNodeContainer MindMapNodeTemp = null; foreach (Control ControlItem in Chidren_Panel.Controls) { MindMapNodeTemp = (MindMapNodeContainer)ControlItem; if (MindMapNodeParame == MindMapNodeTemp) { MindMapNodeTemp.Parent = null; MindMapNodeTemp.ParentNode = null; if (RemoveChidrenNode != null) { RemoveChidrenNode(this, MindMapNodeTemp); } break; } } this.ResetNodeSize(); }
/// <summary> 添加一个节点 /// /// </summary> public void AddNode(MindMapNodeContainer MindMapNodeParame) { if (MindMapNodeParame == null) { return; } List <MindMapNodeContainer> MindMapNodeList = GetChidrenNode(); int FindCount = MindMapNodeList.Where(T1 => T1 == MindMapNodeParame).Count(); if (FindCount != 0) { return; //如果要添加的节点已经存在就直接返回 } if (NodeSizeChanged != null) { NodeSizeChanged(); //隐藏思维导图用以跳过绘制过程 } MindMapNodeContainer NewNode = MindMapNodeParame; Chidren_Panel.Controls.Add(NewNode); MindMapNodeParame.ParentNode = this; MindMapNodeParame.CurrentScaling = this.CurrentScaling; ResetNodeSize(); //添加节点后禁用Tab键。否则Tab键无法触发到MindMap_Panel控件中 this.GetAllControl().ForEach(T1 => T1.TabStop = false); this.TabStop = false; if (AddChidrenNode != null) { AddChidrenNode(this, NewNode); } }
/// <summary>添加节点内容时发生 /// /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void MindMapNodeAddContent(MindMapNodeContainer Sender, MindMapNodeContainer e) { SetEvent(Sender); if (MindMapAddContent != null) { MindMapAddContent(Sender, null); } }
/// <summary> 当节点被移除时发生 /// /// </summary> /// <param name="Sender">发生事件的节点</param> /// <param name="Chidren">被移除的节点</param> private void MindMapNodeRemoveChidrenNode(MindMapNodeContainer Sender, MindMapNodeContainer Chidren) { AddOrRemoveEvent(Chidren, false); if (MindMapRemoveNode != null) { MindMapRemoveNode(Sender, Chidren); } }
/// <summary> 为思维导图绑定数据 /// /// </summary> /// <typeparam name="NodeContent">采用哪种内容布局</typeparam> /// <typeparam name="DataEntity">数据的模型</typeparam> /// <param name="DataSource">数据源</param> /// <param name="ParentID">父ID,留空则表示智能获取根节点ID</param> /// <returns>返回添加后的节点容器</returns> public List <MindMapNodeContainer> SetDataSource <NodeContent, DataEntity>(List <DataEntity> DataSource, string ParentID = null) where NodeContent : MindMapNodeContentBase, new() { if (DataStruct == null) { throw new Exception("DataStruct为空:你需要先指定数据源的结构,再绑定数据源"); } PropertyInfo IDProperty = typeof(DataEntity).GetProperty(DataStruct.MindMapID); PropertyInfo ParentProperty = typeof(DataEntity).GetProperty(DataStruct.MindMapParentID); List <DataEntity> CurrentAddList; bool IsBaseNode = string.IsNullOrEmpty(ParentID); if (IsBaseNode) { InitBaseNode();//如果是根节点就初始化根节点 CurrentAddList = new List <DataEntity>(); CurrentAddList.Add(GetBaseData <DataEntity>(DataSource)); #region MyRegion #endregion } else { CurrentAddList = DataSource.Where(T1 => ParentProperty.GetValue(T1).ToString() == ParentID).ToList(); } List <MindMapNodeContainer> ContainerList = new List <MindMapNodeContainer>(); foreach (DataEntity AddDataItem in CurrentAddList) { string CurrentId = IDProperty.GetValue(AddDataItem).ToString(); List <MindMapNodeContainer> ContainerListTemp = SetDataSource <NodeContent, DataEntity>(DataSource, CurrentId); MindMapNodeContainer NewNode = new MindMapNodeContainer(); if (IsBaseNode) { NewNode = mindMapNode; //如果没有父节点就赋值根节点 } NewNode.SetNodeContent <NodeContent>(DataStruct); ContainerListTemp.ForEach(item => NewNode.AddNode(item)); NewNode.NodeContent.DataItem = AddDataItem; ContainerList.Add(NewNode); } if (IsBaseNode) { SetEvent(mindMapNode);//所有节点都绑定完了就统一为这些节点绑定事件 ScrollCenter(); } return(ContainerList); }
/// <summary> 判断是否允许拖动到指定节点下[不允许拖动到被选中节点和它的子节点下] /// /// </summary> /// <param name="DragedContainer"></param> /// <returns></returns> private bool AllowDrag(MindMapNodeContainer DragedContainer) { #region 获取所有选中节点和他的子节点 List <MindMapNodeContainer> NodeContainerList = GetSelectedNode(); //获取所有选中节点 List <MindMapNodeContainer> SelectedAndChidrenList = new List <MindMapNodeContainer>(); SelectedAndChidrenList.AddRange(NodeContainerList); //获取选中节点 NodeContainerList.ForEach(T1 => SelectedAndChidrenList.AddRange(T1.GetChidrenNode(true))); //获取选中节点的子节点 SelectedAndChidrenList = SelectedAndChidrenList.Distinct().ToList(); //去重,两个已选中节点可能是父子关系 #endregion 获取所有选中节点和他的子节点 if (SelectedAndChidrenList.Contains(DragedContainer))//不允许拖动到被选中节点和它的子节点下 { return(false); } return(true); }
/// <summary> 递归判断是否有父节点存在于集合中 /// /// </summary> /// <param name="ContaineListPatam"></param> /// <param name="CurrentNode"></param> /// <returns></returns> private bool ParentContaine(List <MindMapNodeContainer> ContaineListPatam, MindMapNodeContainer CurrentNode) { if (CurrentNode == null) { return(false); } if (ContaineListPatam.Contains(CurrentNode)) { return(true); } else { return(ParentContaine(ContaineListPatam, CurrentNode.ParentNode)); } }
private void InitBaseNode() { if (mindMapNode != null) { mindMapNode.GetAllControl().ForEach(ControlItem => ControlItem.Dispose()); //把老数据的资源全部释放 mindMapNode.Dispose(); //释放资源 } this.mindMapNode = new MindMapNodeContainer(); this.mindMapNode.BackColor = System.Drawing.Color.White; this.mindMapNode.Location = new System.Drawing.Point(181, 166); this.mindMapNode.Name = "mindMapNode"; this.mindMapNode.ParentNode = null; this.mindMapNode.Size = new System.Drawing.Size(86, 23); this.mindMapNode.TabIndex = 0; this.mindMapNode.EmptyRangeMouseClick += new MouseEventHandler(this.mindMapNode_EmptyRangeMouseClick); this.mindMapNode.EmptyRangeMouseDown += new System.Windows.Forms.MouseEventHandler(this.mindMapNode_EmptyRangeMouseDown); this.mindMapNode.EmptyRangeMouseUp += new System.Windows.Forms.MouseEventHandler(this.mindMapNode_EmptyRangeMouseUp); this.mindMapNode.EmptyRangeMouseMove += new System.Windows.Forms.MouseEventHandler(this.mindMapNode_EmptyRangeMouseMove); this.mindMapNode.RemoveChidrenNode += new MindMapNodeContainer.MindMapEventHandler(MindMapNodeRemoveChidrenNode); this.mindMapNode.Resize += new System.EventHandler(this.mindMapNode_Resize); this.Scroll_panel.Controls.Add(this.mindMapNode); }
/// <summary> 为节点添加或删除事件 /// /// </summary> /// <param name="MindMapContainerParame">要添加或删除的节点容器</param> /// <param name="AddEvent">[true:添加事件;false:删除事件]</param> private void AddOrRemoveEvent(MindMapNodeContainer MindMapContainerParame, bool AddEvent) { if (MindMapContainerParame == null) { return; } //节点容器添加事件 List <MindMapNodeContainer> NodeContainsList = MindMapContainerParame.GetChidrenNode(true); //获取所有节点容器 NodeContainsList.Add(MindMapContainerParame); //包括自己 #region 为节点容器添加事件 List <Control> NodeContentList = new List <Control>(); //用List来收集所有节点内容的控件 NodeContainsList.ForEach(NodeItem => { if (AddEvent) { NodeItem.EmptyRangeMouseClick += new MouseEventHandler(mindMapNode_EmptyRangeMouseClick); NodeItem.EmptyRangeMouseDown += new MouseEventHandler(mindMapNode_EmptyRangeMouseDown); NodeItem.EmptyRangeMouseMove += new MouseEventHandler(mindMapNode_EmptyRangeMouseMove); NodeItem.EmptyRangeMouseUp += new MouseEventHandler(mindMapNode_EmptyRangeMouseUp); NodeItem.AddChidrenNode += new MindMapNodeContainer.MindMapEventHandler(MindMapNodeAddChidrenNode); NodeItem.RemoveChidrenNode += new MindMapNodeContainer.MindMapEventHandler(MindMapNodeRemoveChidrenNode); NodeItem.AddNodeContent += new MindMapNodeContainer.MindMapEventHandler(MindMapNodeAddContent); NodeItem.NodeSizeChanged += new Action(HideMindMap); } else { //避免重复添加委托队列 NodeItem.EmptyRangeMouseClick -= new MouseEventHandler(mindMapNode_EmptyRangeMouseClick); NodeItem.EmptyRangeMouseDown -= new MouseEventHandler(mindMapNode_EmptyRangeMouseDown); NodeItem.EmptyRangeMouseMove -= new MouseEventHandler(mindMapNode_EmptyRangeMouseMove); NodeItem.EmptyRangeMouseUp -= new MouseEventHandler(mindMapNode_EmptyRangeMouseUp); NodeItem.AddChidrenNode -= new MindMapNodeContainer.MindMapEventHandler(MindMapNodeAddChidrenNode); NodeItem.RemoveChidrenNode -= new MindMapNodeContainer.MindMapEventHandler(MindMapNodeRemoveChidrenNode); NodeItem.AddNodeContent -= new MindMapNodeContainer.MindMapEventHandler(MindMapNodeAddContent); NodeItem.NodeSizeChanged -= new Action(HideMindMap); } if (NodeItem.NodeContent != null) { NodeContentList.AddRange(NodeItem.NodeContent.GetEventControl()); //获取当前节点内容的所有控件 } }); #endregion 为节点容器添加事件 #region 为节点内容添加事件 NodeContentList.ForEach(ControlItem => { if (AddEvent) { ControlItem.MouseDown += new MouseEventHandler(mindMapNode_MindMapNodeMouseDown); ControlItem.MouseUp += new MouseEventHandler(mindMapNode_MindMapNodeMouseUp); ControlItem.MouseMove += new MouseEventHandler(mindMapNode_MindMapNodeMouseMove); ControlItem.MouseEnter += new EventHandler(mindMapNode_MindMapNodeMouseEnter); ControlItem.MouseLeave += new EventHandler(mindMapNode_MindMapNodeMouseLeave); ControlItem.MouseDoubleClick += new MouseEventHandler(mindMapNode_MouseDoubleClick); ControlItem.AllowDrop = true;//允许接收拖拽行为 ControlItem.DragOver += new DragEventHandler(MindMapNode_DragOver); ControlItem.DragEnter += new DragEventHandler(MindMapNode_DragEnter); ControlItem.DragDrop += new DragEventHandler(MindMapNode_DragDrop); } else { //避免重复添加委托队列 ControlItem.MouseDown -= new MouseEventHandler(mindMapNode_MindMapNodeMouseDown); ControlItem.MouseUp -= new MouseEventHandler(mindMapNode_MindMapNodeMouseUp); ControlItem.MouseMove -= new MouseEventHandler(mindMapNode_MindMapNodeMouseMove); ControlItem.MouseEnter -= new EventHandler(mindMapNode_MindMapNodeMouseEnter); ControlItem.MouseLeave -= new EventHandler(mindMapNode_MindMapNodeMouseLeave); ControlItem.MouseDoubleClick -= new MouseEventHandler(mindMapNode_MouseDoubleClick); ControlItem.DragOver -= new DragEventHandler(MindMapNode_DragOver); ControlItem.DragEnter -= new DragEventHandler(MindMapNode_DragEnter); ControlItem.DragDrop -= new DragEventHandler(MindMapNode_DragDrop); } }); #endregion 为节点内容添加事件 }
private void SetEvent(MindMapNodeContainer MindMapContainerParame) { AddOrRemoveEvent(MindMapContainerParame, false); //先删除事件,避免重复添加 AddOrRemoveEvent(MindMapContainerParame, true); //添加事件 }