Vra

I am modifying a Windows app that uses System.Windows.Control UI elements,
I want to let the user select an item from a hierarchy, so something that looks like this:

tree

Except it is not folders, but a huge hierarchy of remote objects, very deep and slow to retrieve.
Loading the whole hierarchy before showing it would be too slow, I have to show the first level, and then show inside a folder when the user clicks on it.

I am thinking of using a System.Windows.Control.TreeView and intercept selection events on TreeViewItem elements to populate them when needed.
QUESTION: Is it the best practice strategy, or is there a System.Windows.Control UI element that is more appropriate for this?

A component that reads the content of each folder of the element clicked would be acceptable too.
The app uses .NET 4.5.

Was dit nuttig?

Oplossing

If I understand correctly, you want child nodes to be loaded only when the parent node is expanded (e.g. by clicking its expand icon). Loading children upon node expansion indeed sounds better than loading them upon node selection, from a UX standpoint.

I'm not aware of anything that does that out of the box, but you can easily build one yourself. The approach I saw for this is to add a dummy node under each node whose children haven't been fetched yet, and in the event handler for expanding a node, check if it has a dummy node, and if so load its actual child nodes and replace the dummy with them.

The dummy node causes its parent to appear to be expandable (e.g. have a '+' icon or collapsed triangle), which is probably what you need. If you put "(Loading...)" or some such as the title of the dummy, you'll get a decent user experience out of it.

Ander wenke

1) Create the TreeView:

System.Windows.Controls.TreeView treeView = new System.Windows.Controls.TreeView();
ContentCanvas.Children.Add(treeView);

2) Insert the root item(s):

System.Windows.Controls.TreeViewItem root = new System.Windows.Controls.TreeViewItem();
root.Header = repository;
treeView.Items.Add(root);

3) When one item is selected, add sub-item(s) as needed:

treeView.SelectedItemChanged += delegate
{
    TreeViewItem item = (TreeViewItem)treeView.SelectedItem;
    SparkleLogger.LogInfo("bla", "object:"+item);
    System.Windows.Controls.TreeViewItem subItem = new System.Windows.Controls.TreeViewItem();
    subItem.Header = "hello";
    item.Items.Add(subItem);
};

Addition must be done at the first selection only, otherwise elements will be duplicated.
Any better solution would be warmly welcome!

Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top