Hi, I'm building an application and I use a Datatree to represent a portion of the file system
I'm using the databinding of an Observable collection of FolderElements, Each item contains two collections
Subfolders and Files
The tree works perfectly representing my nodes and files, what I wanted to know is if it is possible
to Hide the Folders and Files nodes when they are empty.
The following is an example:
[root directory]
[subfolders]
[Folder 1]
[subfolders] *
[Files]
[File 10]
[File 11]
[Folder 2]
[Folder n1]
[files]
[file n1]
[files]*
[File on root 1]
[file on root 2]
The Nodes marked wit the * don't have any child node, I was wondering if it is possible to avoid showing them.
The nodes are Part of a Wpf class and are represented by Dependency Properties ObservableCollection<FolderElement> and ObservableCollection<FileElement>
The two classes are dependency objects and their properties are dependency properties.
Is there a way to Hide the Empty objects?
or if this is not possible, Is it possible to Create the Node items from code behind instead of using the databinding allowing us to create them only when needed?
Thank you in advance
Sabrina
Hello Sabrina,
Thank you for your post.
To hide nodes without children, I would recommend handling the InitializeNode event on the XamDataTree. This will fire for each child node when its parent node gets expanded. Using the event arguments of the event handler for the InitializeNode event, you can obtain the node that is initializing from e.Node. From there, you can check the count property of that node's Nodes collection. If it is 0, the node has no children, and you can call e.Node.Control.Visibility = Visibility.Collapsed to hide the node.
You may need to place the e.Node.Control.Visibility setting inside of a Dispatcher.BeginInvoke(new Action(() { visibility code here }))); This is because there is sometimes a small timing issue where the node control is not yet created, and so a null reference exception is thrown on the visibility setting. Placing the visibility code in a dispatcher avoids this.
I hope this helps you. Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate DeveloperInfragistics Inc.www.infragistics.com/support
Hi, After a series of tries I'm sorry to say it does not work (previous answer and question was written before i tried sorry)
The problem is that I don't know why or how, The XamDatagrid creates a "Phantom" node for each collection contained in the node, putting then the collection elements inside that phantom node.
This phantom node does not raise the InitializeNode Event and also, when I inspect the Data content of the nodes from the parent node InitializeNode, the two phantom nodes are present but they contain a casual kind of data type...
The Node containing Subfolders lists itself as a FolderElement (its elements are indeed of that type but it is not a single element, it is a collection).
The Node containing Files lists itself as a FolderElement too (but it is a collection of FileElement)
So I find very difficult to manage things as I need, and watching your samples, unfortunately there is nothing helpful so my question is again
Is there a sample of how to manually build the tree nodes inside a xamDataTree so that I can build it as needed?
thank you in advance
Hello Sabrina,I have been looking into this "Phantom" node that you are referring to, and I have reproduced this behavior in the XamDataTree. This is unexpected, as the nodes that represent collections are still nodes, and so they should be firing the InitializeNode event.
Regarding the visibility issue, I have crafted a workaround for you on this. I also think it may be a better solution than the InitializeNode event as well, as it does not involve any code-behind. I would recommend that you write a Style for XamDataTreeNodeControl. In this Style, you can use a DataTrigger bound to the Node.Nodes collection of the XamDataTreeNodeControl. From there, you can use a converter to check the count of this nodes collection that you are passing to it, and return a boolean value that represents whether the count is 0 or not. If it is, you can use a setter to set the Visibility of this XamDataTreeNodeControl to Collapsed.
Regarding the different icons issue, I have obtained the collection nodes in code, but after setting the Collapsed/Expanded IconTemplates on them, it appears that only the CollapsedIconTemplateResolved template is respected. I believe your best option on this would be to not set the Collapsed/Expanded icon templates in your node layouts, but to return to that XamDataTreeNodeControl style described above.
Using a series of MultiDataTriggers, you can set a ContentTemplate on each of the XamDataTreeNodeControls. The MultiDataTriggers can check the content of the node, as well as whether it is expanded and apply a content template with an image and a textblock in it. This will achieve the functionality you are looking for.
I have attached a sample application to demonstrate the two procedures above.
Now, regarding the InitializeNode event not firing for the nodes that represent collections, I have asked our engineering staff to examine this issue further. To ensure that it will receive attention, I have logged this behavior in our internal tracking system with a development ID of 189253.
I have created a private case for you so that you can track the progress of this development issue. The case number is CAS-151375-L1R3F9 and you can access it here: https://www.infragistics.com/my-account/support-activity.
Please let me know if you have any other questions or concerns on this matter.
Hi Andrew,
I tried your solution for hiding the "Layout" node if it does not have any Chld Nodes, but it doesn't work.. Is there any other setting that needs to happen?