TreeListBox Displaying Recursive Relationships

Grids for WPF Forum

Posted 3 years ago by Rick - Developer, Visual Software Systems LLC
Version: 21.1.1
Avatar

I'm not sure if this is a problem or if there something different I need to do to show recursive relationships properly.  What I've noticed is that TreeListBox seems to get all confused with how to display a node when it's underlying item part of a recursive relationship.  Here's a very simple example: say I have a database schema that I'm representing in the TreeListBox.  The top node is the database name, children are the tables and each table's children are the columns. This works perfectly.  So let's say there's an Orders table and an OrderLineItem table. One of the columns in the OrderLineItem table, OrderId, is a foreign key to the Orders table.  I want to display the Orders table and its columns as a child of the OrderId node in the TreeListBox so I add the Orders object I already have as a child of the OrderId treenodemodel item.  This is where things start to go sideways.  When I expand the OrderId node, the Orders object displays.  When I expand the nested Orders node, the identation is incorrect and the Orders node under the Database node also expands.  It's like TreeListBox has bound the same instance of it's treelistboxitem to both places because the underlying object in the Items collection is the same.

I tried a few ways to resolve this:  I set virtualization to standard on the TreeListBox control but that had no effect.  I created a shallow copy of the underlying Order object and added that to the OrderId column's treenode children collection, but that just caused runtime data binding errors.  Finally, I created a partial copy of the Orders object and added that to the OrderId treenode's children collection.  It works correctly but it's a sub-optimal solution because now I'm creating thousands of object clones (in my real-world application) and using up system resources that I really shouldn't need to.  Any suggestions?  I have a small project that illustrates this.  Just let me know how to get it to you.

[Modified 3 years ago]

Comments (2)

Answer - Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Rick,

Hmm, that might be a tricky scenario.  TreeListBox manages its data items based on equality evaluation.  If you add a second instance of the same object (as far as Equals() returns) then it will run into issues.  Nothing can really change with that design since it's how we map the tree structure to the "flat" list of container items in the TreeListBox.  

Thus you would need to somehow work around that limitation somehow.  Perhaps you could create some kind of "wrapper" node item that would take one of your real "top-level" nodes and pass back all of its properties.  That "wrapper" node item type could be used when you were in a nested scenario, to avoid them being "Equals()" to the real "top-level" nodes?

Any sample projects would have to be sent through our ticket system.


Actipro Software Support

Posted 3 years ago by Rick - Developer, Visual Software Systems LLC
Avatar

I landed up using a wrapper class like you suggested to get around the issues.  It took me awhile to adjust my thinking on how and when to create the wrapper objects but once I realized that I didn't have to create them at the same time I was loading the real objects and could instead only create if and when needed, everything fell into place and I now have a very neat working solution.  Thank you!  

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.