我有这样一个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