private void AddElement <T>() where T : Element { if (treeView.SelectedNode != null) { var elementTreeNode = treeView.SelectedNode as ElementTreeNode; if (elementTreeNode != null) { var groupElement = elementTreeNode.Element as GroupElement; var element = Activator.CreateInstance <T>(); element.Name = $"New {element.GetType().Name}"; groupElement.SubElements.Add(element); var treeNode = new ElementTreeNode(); treeNode.ParentTreeNode = elementTreeNode; treeNode.Element = element; treeNode.Name = element.Name; treeNode.Text = element.Name; treeNode.ParentGroupElement = elementTreeNode.Element as GroupElement; treeNode.ContextMenuStrip = element is GroupElement ? groupContextMenuStrip : normalContextMenuStrip; elementTreeNode.Nodes.Add(treeNode); elementTreeNode.Expand(); treeView.SelectedNode = treeNode; } } }
private void ProcessGroupElement(GroupElement modelBaseGroupElement, ElementTreeNode parentTreeNode) { foreach (var element in modelBaseGroupElement.SubElements) { var elementTreeNode = new ElementTreeNode { Name = element.Name, Text = element.Name, Element = element, ParentGroupElement = modelBaseGroupElement }; elementTreeNode.ContextMenuStrip = normalContextMenuStrip; elementTreeNode.ParentTreeNode = parentTreeNode; if (element is GroupElement) { var groupElement = element as GroupElement; ProcessGroupElement(groupElement, elementTreeNode); elementTreeNode.ContextMenuStrip = groupContextMenuStrip; } parentTreeNode.Nodes.Add(elementTreeNode); } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog(); var dialogResult = openFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { treeView.Nodes.Clear(); IModelLoader modelLoader = new XmlModelLoader(openFileDialog.FileName); _modelBaseGroupElement = modelLoader.LoadModel(); var elementTreeNode = new ElementTreeNode { Element = _modelBaseGroupElement, Name = _modelBaseGroupElement.Name, Text = _modelBaseGroupElement.Name }; elementTreeNode.ContextMenuStrip = groupContextMenuStrip; ProcessGroupElement(_modelBaseGroupElement, elementTreeNode); treeView.Nodes.Add(elementTreeNode); } }