我有這樣一個xml:綁定XML數據到一個TreeView
<root>
<settings>
....
...
..
</settings>
<cards>
<card name="firstcard">
<question>bla</question>
<answer>blub</answer>
</card>
<card name="nextcard">
<question>bla</question>
<answer>blub</answer>
</card>
</cards>
</root>
,我將其綁定到一個TreeView顯示我卡的名字和子項。另外我會將它綁定到一個文本框來編輯節點(問題,答案)。我發現在計算器的描述:Two-way binding of Xml data to the WPF TreeView但我不能將其更改爲我的需求:-( 下面是我的最後一次嘗試:
<Window.Resources>
<HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=card}">
<TextBox Text="cards" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="card">
<StackPanel>
<TextBox Text="{Binding XPath=question}"></TextBox>
<TextBox Text="{Binding XPath=answer}" Margin="0,0,0,15"></TextBox>
</StackPanel>
</HierarchicalDataTemplate>
<XmlDataProvider x:Key="dataxml" XPath="root/cards" Source="path\cards.xml" />
</Window.Resources>
..
...
<Label Content="question:"/>
<TextBox DataContext="{Binding ElementName=treeView, Path=SelectedItem}"
Text="{Binding XPath=question, UpdateSourceTrigger=PropertyChanged}"/>
<Label Content="answer:"/>
<TextBox DataContext="{Binding ElementName=treeView, Path=SelectedItem}"
Text="{Binding XPath=answer, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
<Grid>
<TreeView Name="treeView" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}" />
</Grid>
究竟是什麼問題與您的代碼? – 2011-03-01 13:24:19
我不知道如何將屬性名稱綁定到文本框,並且樹視圖和文本框之間的綁定不起作用。 – jwillmer 2011-03-01 13:33:07