Page 1 of 1

How to create TreeView content programmatically?

Posted: 07 Jul 2014, 12:56
by pmike
We found one method SetItemSource() to create TreeView items, but how to use this method to create hierarchical collections?

Re: How to crate TreeView content programmatically?

Posted: 07 Jul 2014, 13:13
by sfernandez
To correctly set TreeView's content from ItemsSource and using data templates, you will need HierarchicalDataTemplates, that are not implemented yet (please report it in the bugtracker to be notified of the progress on this feature).
    <Grid.Resources>
        ...
        <HierarchicalDataTemplate DataType="DataCategory" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Red"/>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="DataElement">
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </Grid.Resources>
So the only solution available right now to achieve what you want, is to iterate your data structures, and programmatically create a TreeViewItem for each data item and manually assign the appropriate DataTemplate. I could help with this code if required.

Re: How to crate TreeView content programmatically?

Posted: 07 Jul 2014, 13:44
by pmike
Thanks for the answer! We will combine data and XAML in TreeView representation.

Re: How to create TreeView content programmatically?

Posted: 10 Jul 2014, 23:48
by jsantos
Anyway, HierarchicalDataTemplates must be implemented. Please, file a bug to track this feature if it is important for you.