I am trying to create a treeview in WPF from an XDocument. So far I can create the tree with all nodes and values. Now I would like to add all attributes. Here my problems start ;-)
The relevant XAML looks like this:
<Window.Resources>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Focusable="False">
<TextBlock x:Name="tbName" Text="dummy" />
<ItemsControl ItemsSource="{Binding Attributes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Name}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<Binding Path="Elements" />
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=FirstNode.NodeType}" Value="Text">
<Setter TargetName="tbName" Property="Text">
<Setter.Value>
<MultiBinding StringFormat="{}{0} = {1}">
<Binding Path="Name"/>
<Binding Path="FirstNode.Value"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</Window.Resources>
...
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TreeView x:Name="LizenzAnsicht"
ItemsSource="{Binding Path=Root.Elements}"
ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
/>
</ScrollViewer>
The Code behind loading the XDocument looks like this:
LadeDatei.LizenzProjekt = XDocument.Load(@"C:\Users\Bernd\Documents\trash\theXMLFile.xml");
LizenzAnsicht.DataContext = LadeDatei.LizenzProjekt;
As said the tree structure looks quite good except that no attributes show up. Visual Studio show some errors in the Direktfenster (direct windows?):
System.Windows.Data Error: 40 : BindingExpression path error: 'Attributes' property not found on 'object' ''XElement' (HashCode=51812368)'. BindingExpression:Path=Attributes; DataItem='XElement' (HashCode=51812368); target element is 'ItemsControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
Any hints what I need to do to get it working?
Regards Bernd
Attributes()
is a method of XElement. Binding to methods is not supported.I would use a converter to get attributes from XElement for binding purpose:
and