2

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;
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Ryan
  • 791
  • 16
  • 31
  • is it a typo on your MainData Itemssource Binding to Number*1* ? – Markus Hütter Jun 04 '10 at 21:38
  • No it is not. Since I am using the same node 'Number' for the inner node levels, Xsd2Code uses 'Number' for the outer level and 'Number1' for all inner levels. – Ryan Jun 07 '10 at 17:45

1 Answers1

1

Probably you already have solved this, but for me, DataTemplates (HierarchicalDataTemplate as well) never worked properly, if I had defined x:Key and DataType at the same time. This question asks about DataTemplates' DataType:

This property that is very similar to the TargetType property of the Style class. When you set this property to the data type without specifying an x:Key, the DataTemplate gets applied automatically to data objects of that type. Note that when you do that the x:Key is set implicitly. Therefore, if you assign this DataTemplate an x:Key value, you are overriding the implicit x:Key and the DataTemplate would not be applied automatically.

I ended up creating one DataTemplate with x:Key and second one with DataType, which had just one ContentControl that had ContentTemplate set to DataTemplate with x:Key.

Community
  • 1
  • 1
mnn
  • 1,952
  • 4
  • 28
  • 51