We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

ChildNodes not showing up when setting items in code behind

Hello,

I'm trying to create a multicolumn TreeView showing multiple properties of a complex object, but if I set the ItemsSource in code, for some reason the child nodes of nodes are not detected, so there is no expander to expand the subitems (which have the same type as the parent object, so it's like a nested folder structure). Setting the TreeViewItemAdv collection in the XAML works fine.

Here's the XAML:

<Window x:Class="MultiColumnTreeViewAdvSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MultiColumnTreeViewAdvSample"
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
        Title="MainWindow" Height="350" Width="740">
    <Window.Resources>
        <Style TargetType="{x:Type syncfusion:TreeViewItemAdv}">
            <Setter Property="IsExpanded" Value="True"/>
        </Style>
    </Window.Resources>
    <Grid>
        <!-- Adding TreeViewAdv with Enabling multiple column -->
        <syncfusion:TreeViewAdv x:Name="treeViewAdv" MultiColumnEnable="True">

            <!-- Adding TreeViewItemAdv -->
            <!-- Setting the TreeViewItemAdv collection here works, but if this is commented out, and the collection is filled in code, childnodes are not detected -->
            <!--<syncfusion:TreeViewItemAdv Tag="1" Name="treeViewItemAdv" Header="Marital Status">
                <syncfusion:TreeViewItemAdv Tag="11" Header="Single" />
                <syncfusion:TreeViewItemAdv Tag="12" Header="Married"/>
                <syncfusion:TreeViewItemAdv Tag="13" Header="Married with Children"/>
            </syncfusion:TreeViewItemAdv>
            <syncfusion:TreeViewItemAdv Tag="2" Header="Baby Vaccines">
                <syncfusion:TreeViewItemAdv Tag="21" Header="Hepatitis B"/>
                <syncfusion:TreeViewItemAdv Tag="22"  Header="Tetanus"/>
                <syncfusion:TreeViewItemAdv Tag="23" Header="Polio"/>
                <syncfusion:TreeViewItemAdv  Tag="24" Header="Measles"/>
            </syncfusion:TreeViewItemAdv >
            <syncfusion:TreeViewItemAdv Tag="3" Header="Country Information">
                <syncfusion:TreeViewItemAdv Tag="31"  Header="Canada"/>
                <syncfusion:TreeViewItemAdv Tag="32"  Header="France"/>
                <syncfusion:TreeViewItemAdv Tag="33"  Header="Germany"/>
                <syncfusion:TreeViewItemAdv Tag="34"  Header="UK"/>
                <syncfusion:TreeViewItemAdv Tag="35"  Header="USA"/>
            </syncfusion:TreeViewItemAdv>-->

            <!-- Adding header -->
            <syncfusion:TreeViewAdv.Columns>
                <syncfusion:TreeViewColumnCollection>
                    <syncfusion:TreeViewColumn Width="400" Header="Name"/>
                    <syncfusion:TreeViewColumn Width="150" Header="Description"/>
                    <syncfusion:TreeViewColumn Width="150" Header="IsValid"/>
                </syncfusion:TreeViewColumnCollection>
            </syncfusion:TreeViewAdv.Columns>
        </syncfusion:TreeViewAdv>
    </Grid>
</Window>

I tried creating TreeViewItemAdv instances with the DataContext set to my ComplexObject (rather than a collection of ComplexObject's), but that didn't seem to help. What am I missing here?


Attachment: MultiColumnTreeViewAdvSample_489e08c8.zip

5 Replies

VR Vijayalakshmi Roopkumar Syncfusion Team August 28, 2016 03:34 PM UTC

Hi Lon,

Thank you for using Syncfusion products.

To display the child nodes of a parent object, need to bind the subitems using the ItemsSource property to the HierarchicalDataTemplate using the ItemTemplate property of TreeViewAdv. Please find the code snippet and screenshot for the same:

Screenshot: 
This shows how the child nodes expand from parent node in TreeViewAdv using HierarchicalDataTemplate 

CodeSnippet: 
C# 
FrameworkElementFactory NewTemplate = new FrameworkElementFactory(typeof(ContentControl)); 
Binding placebindingnew = new Binding(); 
placebindingnew.Path = new PropertyPath(" "); 
placebindingnew.Mode = BindingMode.OneWay; 
NewTemplate.SetBinding(ContentControl.ContentProperty, placebindingnew); 
HierarchicalDataTemplate datatemple4 = new HierarchicalDataTemplate(); 
Binding placebindingnew1 = new Binding(); 
placebindingnew1.Path = new PropertyPath("Children"); 
placebindingnew1.Mode = BindingMode.OneWay; 
datatemple4.ItemsSource = placebindingnew1; 
datatemple4.VisualTree = NewTemplate; 
(sender as TreeViewAdv).ItemTemplate = datatemple4; 
 

XAML: 
<syncfusion:TreeViewAdv.ItemTemplate> 
<HierarchicalDataTemplate ItemsSource="{Binding Children}"> 
<ContentControl Content="{Binding}"/> 
</HierarchicalDataTemplate> 
</syncfusion:TreeViewAdv.ItemTemplate>           
  
We have modified the sample, please download the same from following link: 
Please let us know if you have any other concerns. 
Regards,
Vijayalakshmi V.R.
 



LH Lon Hofman August 29, 2016 07:13 AM UTC

Hello,

Thank you for your answer. At least now we get the child nodes. Two more questions:

1. For about every node there seems to be a data binding error, especially when I try to set every node to IsExpanded = true by the following style:

    <Window.Resources>
        <Style TargetType="{x:Type syncfusion:TreeViewItemAdv}">
            <Setter Property="IsExpanded" Value="True"/>
        </Style>
    </Window.Resources>

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Syncfusion.Windows.Tools.Controls.TreeViewAdv', AncestorLevel='1''. BindingExpression:Path=ShowRootLines; DataItem=null; target element is 'TreeViewAdvVirtualizingPanel' (Name='PART_ItemsPanel'); target property is 'IsShowLine' (type 'Boolean')

This seems to slow down rendering quite a bit for a larger number of objects in the tree. Is there any way we can solve this?

2. In the 2nd and 3rd column there seems to be a tiny margin (1 px probably) for every child node level. Is there an easy way to get all content left aligned?

Thank you,
Lon


VR Vijayalakshmi Roopkumar Syncfusion Team August 30, 2016 05:42 PM UTC

Hi Lon,

Query 1. For about every node there seems to be a data binding error, especially when I try to set every node to IsExpanded = true by the following style:

We are able to reproduce the reported issue and have logged a defect report regarding this. A support incident to track the status of this defect has been created under your account. Please log on to our support website to check for further updates

https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents 

Query 2. In the 2nd and 3rd column there seems to be a tiny margin (1 px probably) for every child node level. Is there an easy way to get all content left aligned?

We have checked the sample and found that there is tiny margin difference in the 2nd column. But we are unable to found out the margin difference in third column. Please have a look for the same from below:

Screenshot:Screenshotnew5 
Please have a look at the screenshot and let us know whether you are meaning this margin difference in second column? And also provide us any screenshot that shows the issue that you are facing in 3rd column. So that it would be helpful for us to proceed further on this.

Regards,
Vijayalakshmi V.R. 



LH Lon Hofman August 31, 2016 06:54 AM UTC

Hello,

Indeed we mean the difference indicated in your screenshot. A similar problem occurs in the third column. I modified the sample so there is a 6 level deep nesting, that shows the problem more clearly (I made a screenshot of that, included in the zip).

Lon

Attachment: MarginProblem_fea90d75.zip


VR Vijayalakshmi Roopkumar Syncfusion Team September 1, 2016 12:57 PM UTC

Hi Lon, 
We have confirmed the reported Margin issue with the TreeViewAdv is a defect and have logged defect report regarding this. A support incident to track the status of this defect has been created under your account. Please log on to our support website to check for further updates

https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents 
Regards,
Vijayalakshmi V.R. 


Loader.
Up arrow icon