Beispiel #1
0
        public int AddNewNode(TreeViewMItem node)
        {
            string SQL = @"insert into [dbo].[TextNode_Table](Path, Content) values(@Path, @Content);
                    select @ID=@@IDENTITY;";

            SqlParameter[] para =
            {
                new SqlParameter("@Path",    SqlDbType.NVarChar),
                new SqlParameter("@ID",      SqlDbType.Int),
                new SqlParameter("@Content", SqlDbType.NVarChar)
            };
            para[0].Value     = node.Path;
            para[1].Direction = ParameterDirection.Output;
            para[2].Value     = "<FlowDocument AllowDrop=\"True\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph></Paragraph></FlowDocument>";

            using (SqlConnection con = new SqlConnection(ConnectString)) {
                SqlCommand cmd = new SqlCommand(SQL, con);
                cmd.Parameters.AddRange(para);
                con.Open();
                cmd.ExecuteNonQuery();
                node.ID = (int)para[1].Value;
            }

            return(node.ID);
        }
Beispiel #2
0
        private void SaveXmlNodes(ItemCollection nodesCollection, XmlWriter textWriter)
        {
            for (int i = 0; i < nodesCollection.Count; i++)
            {
                TreeViewMItem node = nodesCollection[i] as TreeViewMItem;
                textWriter.WriteStartElement(XmlNodeTag);                                // <TreeNode>

                textWriter.WriteAttributeString(XmlNodeIndex, node.ID.ToString());       // ID
                textWriter.WriteAttributeString(XmlNodeTextAtt, node.Header.ToString()); // Header

                if (node.IsExpanded == true)
                {
                    textWriter.WriteAttributeString(XmlNodeExpandState, node.IsExpanded.ToString());////展开状态
                }
                if (node.IsSelected)
                {
                    textWriter.WriteAttributeString(XmlNodeIsSelect, node.IsSelected.ToString());//是否选中
                }
                if (node.Tag != null)
                {
                    textWriter.WriteAttributeString(XmlNodeTagAtt, node.Tag.ToString());
                }


                // add other node properties to serialize here

                if (node.Items.Count > 0)
                {
                    SaveXmlNodes(node.Items, textWriter);
                }
                textWriter.WriteEndElement();
            }
        }
Beispiel #3
0
 private void SetAttributeValue(TreeViewMItem node, string propertyName, string value)
 {
     if (propertyName == XmlNodeTextAtt) //Header
     {
         node.Header = value;
     }
     else if (propertyName == XmlNodeExpandState)
     {
         //ExpandNode = node;
     }
     else if (propertyName == XmlNodeIsSelect)
     {
         //LastSelectNode = node;
     }
     else if (propertyName == XmlNodeTagAtt)
     {
         node.Tag = value;
     }
     else if (propertyName == XmlNodeIndex)
     {
         // 用来标识 这样看XML文件时结构清晰
         node.ID = int.Parse(value);
     }
 }
Beispiel #4
0
        public void LoadTreeViewState(TreeView treeView, string XML)
        {
            using (StringReader stringReader = new StringReader(XML)) {
                using (XmlReader reader = XmlReader.Create(stringReader)) {
                    treeView.Items.Clear();
                    TreeViewMItem parentNode = null;

                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if (reader.Name == XmlNodeTag)
                            {
                                TreeViewMItem newNode        = new TreeViewMItem();
                                bool          isEmptyElement = reader.IsEmptyElement;

                                // loading node attributes
                                for (int i = 0; i < reader.AttributeCount; i++)
                                {
                                    reader.MoveToAttribute(i);
                                    SetAttributeValue(newNode, reader.Name, reader.Value);
                                    //SetTreeViewState(treeView);
                                }

                                // add new node to Parent Node or TreeView
                                if (parentNode != null)
                                {
                                    parentNode.Items.Add(newNode);
                                }
                                else
                                {
                                    treeView.Items.Add(newNode);
                                }

                                // making current node 'ParentNode' if its not empty
                                if (!isEmptyElement)
                                {
                                    parentNode = newNode;
                                }
                            }
                        }
                        // moving up to in TreeView if end tag is encountered
                        else if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            if (reader.Name == XmlNodeTag)
                            {
                                parentNode = parentNode.Parent as TreeViewMItem;
                            }
                        }
                        else if (reader.NodeType == XmlNodeType.XmlDeclaration)   //Ignore Xml Declaration
                        {
                        }
                        else if (reader.NodeType == XmlNodeType.None)
                        {
                            return;
                        }
                        else if (reader.NodeType == XmlNodeType.Text)
                        {
                            parentNode.Items.Add(reader.Value);
                        }
                    }
                }
            }
        }