Bind Any XML document to WPF TreeView

Boris Kleynbok picture Boris Kleynbok · May 7, 2009 · Viewed 11.9k times · Source

I would like to bind any XML document to WPF TreeView using TypeConverter.

My original solution was to use recursion, but when document is large UI is heavily tied up.

Following link talks about TypeConverter but for particular node/element combination: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/edd843b7-b378-4c2d-926f-c053dbd7b340

What if not known what XML document looks like? Since this is for display purposes only I don't care much about functionality at this point I just want to bind XML to a TreeView.

Answer

receptor picture receptor · Apr 28, 2011

It works for other node types with a few simple modifications. First, HierarchicalDataTemplate.ItemsSource Binding XPath must be changed to "child::node()|attribute::*" to allow any child node and any attribute. Then, add DataTriggers for other NodeTypes. The example below worked for me. NOTE that i added icons for various NodeTypes, you might want to remove them:

        <HierarchicalDataTemplate x:Key="NodeTemplate">
        <StackPanel Orientation="Horizontal">
        <Image x:Name="icon" VerticalAlignment="Center" Margin="1,1,4,1"/>
        <TextBlock x:Name="name" Text="" />
        <TextBlock x:Name="inter" Text="" />
        <TextBlock x:Name="value" Text="" />
        </StackPanel>
        <HierarchicalDataTemplate.ItemsSource>
            <Binding XPath="child::node()|attribute::*" />
        </HierarchicalDataTemplate.ItemsSource>
        <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="icon" Property="Source" Value="icons/element.png"></Setter>
                <Setter TargetName="name" Property="Text" Value="{Binding Path=Name}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                <Setter TargetName="icon" Property="Source" Value="icons/text.png"></Setter>
                <Setter TargetName="value" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Attribute">
                <Setter TargetName="icon" Property="Source" Value="icons/attribute.png"></Setter>
                <Setter TargetName="name" Property="Text" Value="{Binding Path=Name}"></Setter>
                <Setter TargetName="inter" Property="Text" Value=": "></Setter>
                <Setter TargetName="value" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="CDATA">
                <Setter TargetName="icon" Property="Source" Value="icons/cdata.png"></Setter>
                <Setter TargetName="value" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Comment">
                <Setter TargetName="icon" Property="Source" Value="icons/comment.png"></Setter>
                <Setter TargetName="value" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="ProcessingInstruction">
                <Setter TargetName="icon" Property="Source" Value="icons/pi.png"></Setter>
                <Setter TargetName="value" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>