views:

147

answers:

0

I am having problems populating my treeview beyond the first level when the xml is using the same class name to have unlimited levels. I have used Xsd2Code to create the object class.

To keep this post from being 3000 lines long, I am including a link for downloading the project. It can be downloaded here

My XML example

<Testing>
  <Numbers>
    <Number val="1">
      <Number val="1.1">
        <Number val="1.1.1">
          <Number val="1.1.2" />
          <Number val="1.1.3" />
          <Number val="1.1.4" />
        </Number>   
      </Number>
      <Number val="1.2" />
      <Number val="1.3" />
      <Number val="1.4" />
    </Number>
    <Number val="2" />
    <Number val="3" />
    <Number val="4" />
  </Numbers>
  <Numbers>
    <Number val="5" />
    <Number val="6" />
    <Number val="7" />
    <Number val="8" />
  </Numbers>
</Testing>

XAML

<Window.Resources>
    <local:TestingXmlData x:Key="TestXML" />
</Window.Resources>

<Grid>
    <Grid.Resources>

        <HierarchicalDataTemplate x:Key="MainData" ItemsSource="{Binding Number1}"
                                  >
            <Border>
                <Label VerticalContentAlignment="Center">
                    <TextBlock Text="{Binding val}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
                </Label>
            </Border>
        </HierarchicalDataTemplate>


        <HierarchicalDataTemplate x:Key="Test" ItemsSource="{Binding Number}"
                                  ItemTemplate="{StaticResource MainData}"
                                  DataType="{x:Type local:Number}"
                                  >
            <Border>
                <Label VerticalContentAlignment="Center">
                    <TextBlock Text="{Binding val}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
                </Label>
            </Border>
        </HierarchicalDataTemplate>

    </Grid.Resources>

    <telerik:RadTreeView ItemsSource="{Binding Source={StaticResource TestXML}, Path=TestingDataSource}"
                         ItemTemplate="{StaticResource Test}"  
                         />
</Grid>

Code Behind

public class TestingXmlData
{
    private Testing testing;

    public TestingXmlData()
    {
        this.TestingDataSource = new ObservableCollection<Number>();

        var t = AppDomain.CurrentDomain.BaseDirectory;
        testing = Testing.LoadFromFile(t + @"../../Test.xml");

        var numberBranch = (from tt in testing.Items
                   select tt.Number).ToList();

        foreach (var num in numberBranch)
            foreach (var entry in num)
                this.TestingDataSource.Add(entry);
    }

    public ObservableCollection<Number> TestingDataSource
    {
        get;
        set;
    }
}