Reputation: 3789
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>
Upvotes: 0
Views: 1377
Reputation: 3789
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>
Upvotes: 1
Reputation: 1043
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
Upvotes: 3