Get XML Attributes in WPF with a TreeView

2019-07-14 03:18发布

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

1条回答
一纸荒年 Trace。
2楼-- · 2019-07-14 03:48

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:

public class XAttributesConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var xe = value as XElement;
        if (xe == null)
            return Enumerable.Empty<XAttribute>();

        return xe.Attributes();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

and

<Window.Resources>
  <wpfDemos:XAttributesConverter x:Key="AttrConverter"/>
<Window.Resources>

<ItemsControl ItemsSource="{Binding Converter={StaticResource AttrConverter}}">
查看更多
登录 后发表回答