简体   繁体   English

WPF C#-树形视图中的XML编辑绑定

[英]WPF C# - XML Edit Binding in Treeview

I think this is a very basic question, but I can't find answer to it. 我认为这是一个非常基本的问题,但我找不到答案。

I got an XML file which loaded into a textblock of a treeview using hierarchical data template: 我得到了一个XML文件,该文件使用分层数据模板加载到树视图的文本块中:

<HierarchicalDataTemplate.Triggers>
  <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
    <Setter TargetName="textBlock1" Property="Text" Value="{Binding Path=Name}"/>
  </DataTrigger>
</HierarchicalDataTemplate.Triggers>

The c# code to open an XML: 用c#代码打开XML:

XmlDocument doc = new XmlDocument();
doc.Load(open.FileName);
XmlDataProvider dp = (XmlDataProvider)this.FindResource("nodes");
dp.Document = doc;
dp.XPath = "*";

When I double click on the textBlock1 a new window appears with a cancel and a save button, and a textbox(what I want to edit) which contains the selected node: 当我双击textBlock1时,将出现一个带有取消和保存按钮的新窗口,以及一个包含所选节点的文本框(我要编辑的内容):

XmlNode selected_xNode = tree.SelectedItem as XmlNode;
openWindow.textBox1.Text = selected_xNode.Name;

This works fine, but I don't know how can I change the selected node name to the textbox.text when I click on the save button? 这可以正常工作,但是当我单击“保存”按钮时,我不知道如何将选定的节点名称更改为textbox.text?

I want something similar to this: 我想要类似的东西:

selected_xNode.Name = textBox1.Text;

I think you need to open the xml file and write to the specified node and then save it again. 我认为您需要打开xml文件并写入指定的节点,然后再次保存。 You can't just try to change the tree. 您不能只是尝试更改树。

XmlDocument doc = new XmlDocument(); 
doc.Load(open.FileName);
  1. First you need to find the element, preferably with LiNq to Xml. 首先,您需要查找元素,最好使用LiNq到Xml。

  2. Then write it back again (replacing, the old one). 然后再次写回(替换旧的)。

  3. Save

You have already the XmlNode so it will propably be sufficient to find the node already. 您已经有了XmlNode,因此足以找到该节点。

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM