本文介绍了Wpf:treeview在xmlnode到treeviewitem转换期间没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我是C#和VS的新手,我正在尝试将XmlNode转换为TreeViewItem,并以递归方式将更改反映到TreeView中,但TreeView未更新/未显示节点。下面是我的TreeView.xaml代码: < TreeView x:Name = DAPITreeView Margin = 5,5, 5,-596 Grid.RowSpan = 11高度= 829 SelectedItemChanged = DAPITreeView_SelectedItemChanged > < TreeView.ItemContainerStyle> < Style TargetType = {x:Type TreeViewItem} > < Setter Property = IsExpanded Value = True /> < / 风格 > < / TreeView.ItemContainerStyle > < / TreeView > 以下是我的TreeView.xaml.cs: private void LoadTreeView( string filename ) { XmlDocument doc = new XmlDocument(); 尝试 { doc.Load(文件名+ 。xml); } catch (Exception Err) { System.Windows.Forms.MessageBox.Show(Err.Message) ); return ; } ConvertXmlNodeToTreeNode(doc,DAPITreeView.Items); // 树状视图展开全部完成 } private void ConvertXmlNodeToTreeNode(XmlNode xmlNode,ItemCollection itemCollection) { TreeViewItem newTreeViewItem = null ; switch (xmlNode.NodeType) { case XmlNodeType.ProcessingInstruction: case XmlNodeType.XmlDeclaration: break ; case XmlNodeType.Element: { newTreeViewItem = new TreeViewItem (); newTreeViewItem.Items.Add(xmlNode.Name); itemCollection.Add(newTreeViewItem); // TreeView未在此处更新 } 断裂; case XmlNodeType.Comment: { newTreeViewItem = new TreeViewItem (); newTreeViewItem.Items.Add(xmlNode.Name); newTreeViewItem = new TreeViewItem(); newTreeViewItem.Items.Add( <! - + xmlNode.Value + - >); } break ; } if (xmlNode.Name == message) { if (xmlNode.ChildNodes.Count > 0 ) { newTreeViewItem = new TreeViewItem(); newTreeViewItem.Items.Add( dummy); } return ; } foreach (XmlNode childNode in xmlNode.ChildNodes) { if (newTreeViewItem == null ) { newTreeViewItem = new TreeViewItem(); itemCollection.Add(xmlNode.Name); // 只有作为#document添加到TreeView的项目。 newTreeViewItem.Items.Add(xmlNode) 。名称); } ConvertXmlNodeToTreeNode(childNode,newTreeViewItem.Items); } } 有谁能建议我说什么是否遗失或需要更正? 我尝试过: 我已经提到了许多博客,但没有针对上述问题的正确解决方案。解决方案 找到更正。感谢您的评论。 newTreeViewItem.Header =( 虚设); // newTreeViewItem.Items.Add(dummy); I am new to C# and VS and am trying to convert XmlNode to TreeViewItem and reflect the changes into TreeView in a recursive manner but TreeView is not updated/ not showing the nodes. Below is my TreeView.xaml code:<TreeView x:Name="DAPITreeView" Margin="5,5,5,-596" Grid.RowSpan="11" Height="829" SelectedItemChanged="DAPITreeView_SelectedItemChanged" > <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="True"/> </Style> </TreeView.ItemContainerStyle> </TreeView>Below is my TreeView.xaml.cs:private void LoadTreeView(string filename){ XmlDocument doc = new XmlDocument(); try { doc.Load(filename + ".xml"); } catch (Exception Err) { System.Windows.Forms.MessageBox.Show(Err.Message); return; } ConvertXmlNodeToTreeNode(doc, DAPITreeView.Items); //Tree View Expand All to be Done}private void ConvertXmlNodeToTreeNode(XmlNode xmlNode, ItemCollection itemCollection){ TreeViewItem newTreeViewItem = null; switch (xmlNode.NodeType) { case XmlNodeType.ProcessingInstruction: case XmlNodeType.XmlDeclaration: break; case XmlNodeType.Element: { newTreeViewItem = new TreeViewItem(); newTreeViewItem.Items.Add(xmlNode.Name); itemCollection.Add(newTreeViewItem); //TreeView not updated here } break; case XmlNodeType.Comment: { newTreeViewItem = new TreeViewItem(); newTreeViewItem.Items.Add(xmlNode.Name); newTreeViewItem = new TreeViewItem(); newTreeViewItem.Items.Add("<!--" + xmlNode.Value + "-->"); } break; } if (xmlNode.Name == "message") { if (xmlNode.ChildNodes.Count > 0) { newTreeViewItem = new TreeViewItem(); newTreeViewItem.Items.Add("dummy"); } return ; } foreach (XmlNode childNode in xmlNode.ChildNodes) { if (newTreeViewItem == null) { newTreeViewItem = new TreeViewItem(); itemCollection.Add(xmlNode.Name); //Only item added to TreeView as #document. newTreeViewItem.Items.Add(xmlNode.Name); } ConvertXmlNodeToTreeNode(childNode, newTreeViewItem.Items); }}Can anyone suggest me what is missing or needs correction?What I have tried:I have referred many blogs but not the right solution for the above problem. 解决方案 Found the correction. Thanks for the comments.newTreeViewItem.Header = ("dummy");//newTreeViewItem.Items.Add("dummy"); 这篇关于Wpf:treeview在xmlnode到treeviewitem转换期间没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-07 22:54