【发布时间】:2011-03-01 13:19:50
【问题描述】:
我有一个这样的 xml:
<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>
我会将它绑定到一个树视图,该树视图向我显示卡片及其名称和子项。此外,我会将其绑定到文本框以编辑节点(问题、答案)。我在 stackoverflow 上找到了描述: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>
【问题讨论】:
-
你的代码到底有什么问题?
-
我不知道如何将属性名称绑定到文本框,并且树视图和文本框之间的绑定不起作用。
标签: c# wpf xml data-binding treeview