Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
960
How to Hide Nodes without children
posted

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]

               [subfolders]

                     [Folder n1]

                        [subfolders] *

                        [files]

                           [file n1]

              [files]*

      [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

Parents
  • 34810
    Offline posted

    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,
    Andrew
    Associate Developer
    Infragistics Inc.
    www.infragistics.com/support

Reply Children