2011-03-01 86 views
0

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

究竟是什么问题与您的代码? – 2011-03-01 13:24:19

+0

我不知道如何将属性名称绑定到文本框,并且树视图和文本框之间的绑定不起作用。 – jwillmer 2011-03-01 13:33:07

回答

1

我有coldandtired :-) 的帮助下解决了这个问题,如果我我可以尔德纪念你的答案有用;-) 下面的工作代码:

<HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=card}"> 
    <TextBox Text="somethings" /> 
</HierarchicalDataTemplate> 

<HierarchicalDataTemplate DataType="card"> 
    <StackPanel> 
     <TextBlock Text="{Binding [email protected]}"/> 
     <TextBlock Text="{Binding XPath=question}"/> 
     <TextBlock Text="{Binding XPath=answer}" Margin="0,0,0,15"/> 
    </StackPanel> 
</HierarchicalDataTemplate> 
... 
.. 
.. 
<XmlDataProvider x:Key="dataxml" XPath="root/cards" Source="folder\cards.xml" /> 

     <Label Height="28" Content="Frage:" Margin="0,0,0,177" /> 
     <TextBox DataContext="{Binding ElementName=treeView, Path=SelectedItem}" Text="{Binding XPath=answer, UpdateSourceTrigger=PropertyChanged}" Margin="0,44,0,136" /> 
     <Label Height="28" Content="Antwort:" Margin="0,102,0,94" /> 
     <TextBox DataContext="{Binding ElementName=treeView, Path=SelectedItem}" Text="{Binding XPath=question, UpdateSourceTrigger=PropertyChanged}" Margin="0,136,0,0" /> 
    </Grid> 
    <Grid> 
     <TreeView Name="treeView" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}"/> 
    </Grid> 
+0

很好听!相当恼人的是,我有几个月前我自己也做了几乎相同的事情,而且我找不到项目的位置:) – coldandtired 2011-03-01 15:02:55

3

因为“名”是一个属性,而不是一个子节点,你需要使用[email protected],使其工作。

有由约什 - 史密斯一个很好的文章here

+0

谢谢,那articel帮助:-) 现在我可以打印出属性。 但与文本框的绑定仍然是一个问题。它只会选择卡的最后一个元素:-( – jwillmer 2011-03-01 14:00:43

+0

我认为问题在于你的TreeView中有TextBoxes,我不太明白,如果你想用TreeView外的TreeView编辑数据,你需要把它们改为TextBlocks,它可以正常工作 – coldandtired 2011-03-01 14:31:04

+0

用我的HierarchicalDataTemplat一个鼠标点击选择问题和答案,这就是为什么我的代码不起作用:-(任何解决方法? – jwillmer 2011-03-01 14:42:33