如何在C#中编辑TreeView中的字段?

时间:2017-02-01 19:31:04

标签: c# wpf treeview xmlnode

我有一个TreeView:

<TreeView Name="OOB"

我知道点击的项目:

 selectedNode = (XmlNode)OOB.SelectedItem;

TreeView是根据这种XML结构构建的:

<?xml version="1.0" encoding="UTF-8"?>

<Army>
    <ArmyName>The Army of Northern Virginia</ArmyName>
     <Commander> 
         <CommanderName>The Emperor Napoleon</CommanderName>
         <CommanderLeadership>94</CommanderLeadership> 
            <Division>
                <DivisionCommanderName>Major General William T. Sherman</DivisionCommanderName>
                <DivisionCommanderLeadership>78</DivisionCommanderLeadership>
                <Unit>
                    <UnitName>Chasseurs à Cheval Garde</UnitName>
                    <UnitIcon>Cavalry.png</UnitIcon>
                    <UnitType>Cavalry</UnitType>
                    <UnitKStrength>3</UnitKStrength>
                    <UnitStrength>456</UnitStrength>
                    <UnitQuality>94</UnitQuality>
                    <UnitMorale>72</UnitMorale>
                    <UnitLeadership>74</UnitLeadership>
                    <UnitAmmunition>99</UnitAmmunition>
                </Unit>
                <Unit>
                    <UnitName>Battery B 4th US Artillery</UnitName>
                    <UnitIcon>Artillery.png</UnitIcon>
                    <UnitType>Artillery</UnitType>
                    <UnitKStrength>3</UnitKStrength>
                    <UnitStrength>4456</UnitStrength>
                    <UnitQuality>88</UnitQuality>
                    <UnitMorale>65</UnitMorale>
                    <UnitLeadership>82</UnitLeadership>
                    <UnitAmmunition>25</UnitAmmunition>
                </Unit>
            </Division>
            <Division>
                <DivisionCommanderName>Spoons Butler</DivisionCommanderName>
                <DivisionCommanderLeadership>18</DivisionCommanderLeadership>

然后,弹出一个对话框并消失后,我想将对话框中的新值放入字段中:

<DivisionCommanderLeadership>78</DivisionCommanderLeadership>

例如。如何将值写入特定的TreeView字段?

谢谢!

1 个答案:

答案 0 :(得分:0)

这解决了问题(实际上对于另一个领域,但是相同的区别): MainWindow.SelectedNode.FirstChild.InnerText = CommanderName.Text;