0

i have a xml like this:

<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>

and i would bind it to a treeview that shows me the cards with their names and subitems. Also i would bind this to a textbox to edit the nodes (question, answer). I have found a description on stackoverflow: Two-way binding of Xml data to the WPF TreeView but i can´t change it to my needs :-( below is my last try:

<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>
Community
  • 1
  • 1
jwillmer
  • 3,570
  • 5
  • 37
  • 73

2 Answers2

3

Because the 'name' is an attribute and not a child node, you'll need to use XPath=@name to make it work.

There's a nice article by Josh Smith here

coldandtired
  • 993
  • 1
  • 10
  • 13
  • thanks, that articel helped :-) now i can print out the attribute. but the binding with the textbox is still a problem. it only selects the last element of a card :-( – jwillmer Mar 01 '11 at 14:00
  • I think the problem is that inside your TreeView you have TextBoxes, which I don't quite understand. If you want to edit the data with a TextBox outside the TreeView you don't need them. Change them to TextBlocks and it works okay. – coldandtired Mar 01 '11 at 14:31
  • with my HierarchicalDataTemplat a mouseclick selects question and answer together, this is why my code doesn´t work :-( any workarounds? – jwillmer Mar 01 '11 at 14:42
1

i have solved it with the help of coldandtired :-) if i could i wold mark your answer as usefull ;-) below the working code:

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

<HierarchicalDataTemplate DataType="card">
    <StackPanel>
        <TextBlock Text="{Binding XPath=@name}"/>
        <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>
jwillmer
  • 3,570
  • 5
  • 37
  • 73
  • Good to hear! Rather annoyingly, I had to do almost the same thing myself a few months' ago and I can't find where I put the project :) – coldandtired Mar 01 '11 at 15:02